Async.map will not call callback in nodejs

I am trying to use async.map, but I can not get it to call a callback for some reason, in the example below, the d function should display the r array, but that just is not the case. in fact it is as if d was never called.

I have to do something really wrong, but I can’t understand what

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

in advance for your help

+8
asynchronous callback
source share
1 answer
 var async = require('async'); //This is your async worker function //It takes the item first and the callback second function addOne(number, callback) { //There no true asynchronous code here, so use process.nextTick //to prove we've really got it right process.nextTick(function () { //the callback 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); 
+14
source share

All Articles