【问题标题】:Invoking async.series inside async.series produces unpredictable output在 async.series 中调用 async.series 会产生不可预测的输出
【发布时间】:2012-09-15 06:18:15
【问题描述】:

使用 caolan 的 node.js 异步库,我一直在尝试在另一个使用 async.series 的函数中调用使用 async.series 的函数,但我仍然无法让函数以正确的顺序运行,具体如下:

终端输出显示第二个函数在第一个函数之前被调用,原因不明:

The "sys" module is now called "util". It should have a similar interface.
Starting the second step in the series
Value of b: undefined
Invoking the function firstStep
the value of toObtain is: [object Object]

下面是对应的源代码:

var im = require('imagemagick');
var async = require('async');

var toObtain;


var b;
async.series([

function (callback) {
    //It appears that this function is being invoked after the second function.
    //Why is this happening?
    firstStep();
    callback();
},

function (callback) {
    //Why is the output of this function being displayed BEFORE the output of the function above? It's the opposite of the order in which I'm calling the functions.
    console.log("Starting the second step in the series");
    console.log("Value of b: " + b);
}]);


function firstStep(){
    async.series([

    function (next) { // step one - call the function that sets toObtain
        im.identify('kittens.png', function (err, features) {
            if (err) throw err;
            console.log("Invoking the function firstStep");
            toObtain = features;
            //console.log(toObtain);
            b = toObtain.height;
            next(); // invoke the callback provided by async
        });
    },

    function (next) { // step two - display it
        console.log('the value of toObtain is: %s',toObtain.toString());
    }]);
}

【问题讨论】:

    标签: node.js


    【解决方案1】:

    经过大约一个小时的实验,我让它正常工作。我只是简单地修改了 firstStep 函数,让它接受一个回调函数作为参数,并在 firstStep 函数结束时调用回调。

    var im = require('imagemagick');
    var async = require('async');
    
    var toObtain = false;
    
    
    var b;
    async.series([
    
    function (callback) {
        firstStep(callback); //the firstStep function takes a callback parameter and calls the callback when it finishes running. Now everything seems to be working as intended.
    },
    
    function (callback) {
        console.log("Starting the second step in the series");
        console.log("Value of b: " + b);
    }]);
    
    
    function firstStep(theCallback){
        async.series([
    
        function (next) { // step one - call the function that sets toObtain
            im.identify('kittens.png', function (err, features) {
                if (err) throw err;
                console.log("Invoking the function firstStep");
                toObtain = features;
                //console.log(toObtain);
                b = toObtain.height;
                next(); // invoke the callback provided by async
            });
        },
    
        function (next) { // step two - display it
            console.log('the value of toObtain is: %s',toObtain.toString());
            theCallback();
        }]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      相关资源
      最近更新 更多