Async: Strange behavior with async.map

Created on 31 Aug 2017  路  2Comments  路  Source: caolan/async

What version of async are you using?
2.5.0

Which environment did the issue occur in (Node version/browser version)
Node v6.11.2

What did you do? Please include a minimal reproducable case illustrating issue.
Normal usage of async.map as documentated.

const async = require('async');

let arr = [ 1, 2, 3, 4, 5 ];
let runCounter = 0;

async.map(arr, (value, callback) => {
  console.log(++runCounter);

  callback(value + 1);
}, (error, result) => {
  if (error) console.error('Error:', error);

  console.log('Result:', result);
});

Note: This bevavior also appears without ES6.

What did you expect to happen?
The values of arr should be incremented by 1 and the result parameter of the callback should contain the new array with incremented values.

What was the actual result?
The actual result of the code above is the following:

1
Error: 2
Result: [ undefined ]
2
3
4
5
question

Most helpful comment

Hi @morphesus, thanks for the question!

The callback passed to the iteratee expects an error, which can be null, as the first argument, and the transformed value as the second.

Changing callback(value + 1); to callback(null, value + 1); gives the expected:

1
2
3
4
5
Result: [2, 3, 4, 5, 6]

See the map docs and the AsyncFunction docs for more info.

All 2 comments

Hi @morphesus, thanks for the question!

The callback passed to the iteratee expects an error, which can be null, as the first argument, and the transformed value as the second.

Changing callback(value + 1); to callback(null, value + 1); gives the expected:

1
2
3
4
5
Result: [2, 3, 4, 5, 6]

See the map docs and the AsyncFunction docs for more info.

Hi @hargasinski!

You're right, I miss understand something from the docs.
Thanks for the response! :)

Was this page helpful?
0 / 5 - 0 ratings