路过的功能阵列并行异步 [英] passing arrays of functions to async parallel

查看:114
本文介绍了路过的功能阵列并行异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的功能的数组传递给node.js中的 async.js 模块< BR>
从文档以正常的方式将是:

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){
});

我想这样的:

            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 isnt called.
and so i cant say if all parallel calls are done.

我失去了什么吗?

推荐答案

看起来扭曲且封锁在for循环。尝试返回您目前分配给FF值的外部函数。例如:

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);
    };
}

我是有点要添加到callitems什么困惑 - 呼叫FF的功能参数也即结果 - 它不会是一个回调,它将执行马上

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.

这篇关于路过的功能阵列并行异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆