我正在尝试使用,但不能在下面的例子中调用callback的一些不明原因,函数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);},f(c),d); 



网上收集的解决方案 "async.map不会在nodejs中调用callback"

 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); }[1,2,3,4,5], addOne, done);