我正在尝试使用async.map但是由于某些unknwon原因无法让它调用回调 在下面的例子中,函数d应该显示数组r,但它不会。 实际上就好像d从未被调用过一样。
我必须做一些非常错误的事情,但无法弄清楚是什么
async = require('async');
a= [ 1,2,3,4,5];
r=new Array();
function f(callback){
return function(e){
e++;
callback(e);}
}
function c(data){ r.push(data); }
function d(r){ console.log(r);}
async.map(a,f(c),d);
提前感谢您的帮助
答案 0 :(得分:14)
var async = require('async');
//This is your async worker function
//It takes the item first and the callback second
function addOne(number, callback) {
//There's no true asynchronous code here, so use process.nextTick
//to prove we've really got it right
process.nextTick(function () {
//the callback's first argument is an error, which must be null
//for success, then the value you want to yield as a result
callback(null, ++number);
});
}
//The done function must take an error first
// and the results array second
function done(error, result) {
console.log("map completed. Error: ", error, " result: ", result);
}
async.map([1,2,3,4,5], addOne, done);