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
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! :)
Most helpful comment
Hi @morphesus, thanks for the question!
The
callbackpassed 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);tocallback(null, value + 1);gives the expected:See the map docs and the AsyncFunction docs for more info.