传递参数中的node.js到async.parallel [英] Passing arguments to async.parallel in node.js

查看:126
本文介绍了传递参数中的node.js到async.parallel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个最小的例子,我可以完成我上面描述。为此,这里是我的一个小例子,到底哪里,我想在输出中看到的尝试

I am attempting to create a minimal example where I can accomplish what I describe above. For this purpose, here is my attempt for a minimal example, where in the end I would like to see in the output

1负为-1

加2一个是3

下面是我的code。

var async = require('async');
var i, args = [1, 2];
var names = ["negative", "plusOne"];

var funcArray = [
    function negative(number, callback) {
        var neg = 0 - number;
        console.log("negative of " + number + " is " + neg);
        callback(null, neg);
    },
    function plusOne(number, callback) {
        setTimeout(function(number, callback) {
            var onemore = number + 1
            console.log("plus one of " + number + " is " + onemore);
            callback(null, onemore);
        }, 3000);
    }];

var funcCalls = {};
for (i = 0; i < 2; i++) {
    funcCalls[names[i]] = function() {
        funcArray[i].apply(this, args[i]);
    };
}

async.parallel(funcCalls, function(error, results) {
    console.log("Parallel ended with error " + error);
    console.log("Results: " + JSON.stringify(results));
});

请注意,我传递了一个名为对象async.parallel为好。传递数组(和完全有关的名字忘了),也将工作作为我一个答案,但我更感兴趣的是传递这样一个对象。

Note that I am passing a named object to async.parallel as well. Passing an array (and forgetting entirely about the names) would also work as an answer for me, but I am more interested in passing such an object.

这是实现我的目标任何想法?

Any ideas on achieving my goal?

推荐答案

为什么不绑定的初始值?然后,你将有这样的事情:

Why not to bind the initial values? Then you would have something like this:

async.parallel([
    negative.bind(null, -1),
    plusOne.bind(null, 3)
], function(err, res) {
    console.log(err, res);
});

当然,你可以产生与各种参数列表。这只是给一个简单的方法的想法。

Of course you could generate the list with various parameters. This was just to give an idea of a simplified approach.

这篇关于传递参数中的node.js到async.parallel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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