0

我正在尝试使用Async.js来触发一系列异步函数。下面是我的代码。只有前两个函数执行。系列中的第三个和第四个函数不执行。我已经将思考的功能简化为最基本的可能。但他们仍然没有执行。有人可以告诉我我做错了什么吗?

async.series([
        guessCollection.find( { user: user, imageFileName: imageFileName } ).count( function(err, number) {
        count = number;
        console.log(count);
        }),

        guessCollection.find( { user: user, imageFileName: imageFileName, correct: '1' } ).count( function(err, number) {
        correct = number;
        console.log(correct);
        }),

        function(){
            console.log("this text never doesn't get logged");
        },
        function() {
            console.log("neither does this text");

        }
    ]);

编辑---正如下面的答案所建议的,我做了前两个适当的功能。然而,现在只执行该系列中的第一个函数。函数 2-4 不会被调用。我认为这段代码肯定还有其他问题。

async.series([
        function(){
        guessCollection.find( { user: user, imageFileName: imageFileName } ).count( function(err, number) {
        count = number;
        console.log(count);
        })
    },
        function(){
        guessCollection.find( { user: user, imageFileName: imageFileName, correct: '1' } ).count( function(err, number) {
        correct = number;
        console.log(correct);
        })
    },

        function(){
            console.log("this text never doesn't get logged");

        },
        function() {
            console.log("neither does this text");

        }
    ]);
4

3 回答 3

4

take a look to this code, it output only 1 2 3, because 3rd function not call the callback function, so series stops here. http://jsfiddle.net/oceog/9PgTS/

​async.series([
    function (c) {
        console.log(1);
        c(null);
    },        
    function (c) {
        console.log(2);
        c(null);
    },        
    function (c) {
        console.log(3);
//        c(null);
    },        
    function (c) {
        console.log(4);
        c(null);
    },        
    ]);​
于 2012-10-21T23:32:26.017 回答
1

You are supposed to provide async.series only with functions. The first items in the array are not functions. You need to wrap these calls in ones.

async.series([
  function () {
    collection.find().count(function () { … });
  },
  function () {
    collection.find().count(function () { … });
  },
  function () {
    console.log();
  },
  function () {
    console.log();
  }
]);
于 2012-10-21T23:24:55.943 回答
0

The first two items in the collection don't look like functions, it looks like you're invoking the first two functions immediately - or does count() return a function?

If you are invoking them, and not passing functions to async that's why it's choking before getting to the last two items.

于 2012-10-21T23:24:42.377 回答