最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论