Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async.map won't call callback in nodejs

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

I must be doing something really wrong but can't figure out 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);

thank you in advance for your help

like image 389
user3097050 Avatar asked Dec 12 '13 20:12

user3097050


1 Answers

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);
like image 84
Peter Lyons Avatar answered Nov 08 '22 10:11

Peter Lyons