9

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

1
  • var fc = f(c); // fc dont have callback param, that the problem Commented Dec 12, 2013 at 21:20

1 Answer 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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.