繁体   English   中英

并非Async.js系列中的每个函数都执行

[英]Not every function in Async.js series executes

我正在尝试使用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");

        }
    ]);

看一下这段代码,它只输出1 2 3,因为第3个函数没有调用回调函数,所以series在这里停止。 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);
    },        
    ]);​

您应该只为async.series提供功能。 数组中的第一项不是函数。 您需要将这些调用包装成一个。

async.series([
  function () {
    collection.find().count(function () { … });
  },
  function () {
    collection.find().count(function () { … });
  },
  function () {
    console.log();
  },
  function () {
    console.log();
  }
]);

集合中的前两个项目看起来不像函数,看起来您正在立即调用前两个函数-还是count()返回一个函数?

如果您要调用它们,而没有将函数传递给异步,这就是为什么在进入最后两个项目之前它会令人窒息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM