javascript - Passing arrays of functions to async parallel - Stack Overflow

I have to pass an array of functions to the async.js module for node.js.The normal way from the docs wo

I have to pass an array of functions to the async.js module for node.js.

The normal way from the docs would be:

async.parallel([
    function(callback){
        setTimeout(function(){
            callback(null, 'one');
        }, 200);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'two');
        }, 100);
    },
],
// optional callback
function(err, results){
});

I tried like this:

for(var i = 0; i < jsonData.length; i++)
{
    ...
    o.url = serviceurl;
    o.title = jsonData[i];
    var ff = function(callback){
        obj.loadService(o.title,o.url,callback);
    }
   callItems.push(ff(function(){return true;}));
}

async.parallel(
callItems,
    // optional callback
    function(err, results){
        console.log('all calls called without any errors');
    }
);

That runs through but the main callback isn't called. And so I can't say if all parallel calls are done.

What am I missing here?

I have to pass an array of functions to the async.js module for node.js.

The normal way from the docs would be:

async.parallel([
    function(callback){
        setTimeout(function(){
            callback(null, 'one');
        }, 200);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'two');
        }, 100);
    },
],
// optional callback
function(err, results){
});

I tried like this:

for(var i = 0; i < jsonData.length; i++)
{
    ...
    o.url = serviceurl;
    o.title = jsonData[i];
    var ff = function(callback){
        obj.loadService(o.title,o.url,callback);
    }
   callItems.push(ff(function(){return true;}));
}

async.parallel(
callItems,
    // optional callback
    function(err, results){
        console.log('all calls called without any errors');
    }
);

That runs through but the main callback isn't called. And so I can't say if all parallel calls are done.

What am I missing here?

Share Improve this question edited Apr 1, 2020 at 13:22 bad_coder 13k20 gold badges56 silver badges88 bronze badges asked Jun 27, 2011 at 18:02 ivobaivoba 6,0366 gold badges52 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It looks like the closures are improperly formed in the for loop. Try an external function that returns the value you're currently assigning to ff. Example:

for(var i = 0; i < jsonData.length; i++)
{
    ...
    o.url = serviceurl;
    o.title = jsonData[i];
    var ff = makeCallbackFunc(obj, o.title, o.url);
    callItems.push(ff(function () {return true;}));
}

function makeCallbackFunc(obj, title, url) {
    return function (callback) {
      obj.loadService(title, url, callback);
    };
}

I'm a bit confused by what you are adding to callitems - namely the result of calling ff with the function parameter - it won't be a callback, it will execute right away.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745377863a4625084.html

相关推荐

  • javascript - Passing arrays of functions to async parallel - Stack Overflow

    I have to pass an array of functions to the async.js module for node.js.The normal way from the docs wo

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信