【问题标题】:async.map won't call callback in nodejsasync.map 不会在 nodejs 中调用回调
【发布时间】:2013-12-31 11:14:11
【问题描述】:

我正在尝试使用 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 没有回调参数,那个问题

标签: node.js asynchronous callback


【解决方案1】:
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);

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多