在Node.js中,async.map不会调用回调函数。

9
我正在尝试使用async.map,但无法调用回调函数,原因未知。在下面的示例中,函数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);

感谢您提前的帮助。


var fc = f(c); // fc没有回调参数,这就是问题 - damphat
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接