NodeJS Async module: how to get results object in the callback?

Problem

Code Snippets:

var async = require("async");

async.map([
    "a",
    "b",
    "c"
], function(thing, callback) {
  console.log(thing + "-something");
  callback();
},
function(err, results){
    if (err) return console.error(err);
    console.log(results);
});

My current results:

a-something
b-something
c-something
[ undefined, undefined, undefined ]

My desired results:

[ a-something, b-something, c-something ]

Could you guys tell me what I am getting wrong? How do I have access to the results object in the callback?

Problem courtesy of: samol

Solution

You need to pass your result into callback() as the second parameter (the first parameter should be the error if there is any):

var async = require("async");

async.map([
    "a",
    "b",
    "c"
], function(thing, callback) {
  var returnValue = thing + "-something";
  console.log(returnValue);
  callback(null, returnValue);
},
function(err, results){
    if (err) return console.error(err);
    console.log(results);
});
Solution courtesy of: go-oleg

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.