在node.js中向async.parallel传递参数
Passing arguments to async.parallel in node.js
我正试图创建一个最小的例子,在这里我可以完成上面描述的内容。为此,这里是我尝试的一个最小的例子,最后我希望在输出中看到
负1是-1
2加1等于3
这是我的密码。
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。传递一个数组(并完全忘记名称)对我来说也是一个答案,但我更感兴趣的是传递这样一个对象。
对实现我的目标有什么想法吗?
为什么不bind
的初始值?然后你会有这样的东西:
async.parallel([
negative.bind(null, -1),
plusOne.bind(null, 3)
], function(err, res) {
console.log(err, res);
});
当然,您可以使用各种参数生成列表。这只是为了给出一个简化方法的想法。
相反,您也可以使用async.apply
async.parallel(
[
async.apply(negative, -1), //callback is automatically passed to the function
async.apply(positive, 3)
],
(error, results) => {
console.log(error, results);
})
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- 使用Express捕获参数
- 参数变量出现ngTable指令问题
- AngularJS:我可以跳过函数参数回调吗
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 要求未定义JS回调参数
- 我的jQuery插件参数没有正确启动,遇到了问题
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- jquery设置为使用参数运行
- Javascript”;类“;带有参数的扩展
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 函数未将值作为参数传递
- 如何将参数传递到angularJs中的工厂
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- MVC 3页面导航和使用javascript传递参数
- 从查询字符串参数推断出正确的数据类型
- 使用 Async.parallel 我的参数的生命周期不会超过使用 MongoDB 的 NodeJS 中的异步调用
- 在node.js中向async.parallel传递参数
- CoffeeScript:我可以在使用async.parallel时传递函数的参数吗?