async.each should include a results array to the callback

Created on 2 May 2016  ·  8Comments  ·  Source: caolan/async

It would be extremely useful to have the each() call include a results array to its callback.

Most helpful comment

That's what async.map is for.

All 8 comments

That's what async.map is for.

Hi,
Indeed map works exactly like you described @aearly.
Although in terms of code readibility I agree with @pcman312 it would be nice to have eachSeries have a callback (err, results).

This would make it consistent with async.series which also has a callback(err, results).

i.e: One may want to run a function for each element in a collection but the goal of this function would not be to "map" values. (in this case, eachSeries or each would be better readability).

Any chance you could still consider implementing it the same as async.series regarding the results returned ? Thanks a lot !

I don't think this is worth considering as this functionality is already 100% covered by mapSeries. If you care about the results of your functions than you are mapping

series and mapSeries are also quite different as series is providing you the result of your last callback whereas mapSeries is mapping the result of each callback in an array

@megawac

are also quite different as series is providing you the result of your last callback

No, series also mapping the result of each callback.

Whoops my bad, was mixing it up with waterfall. Anyway, my comment still stands in regards to mapSeries

Sounds good guys, no problem, probably needs to get used to the syntax :-)
Thanks for your time and for the great library in any case !

each is also slightly faster since it doesn't have to keep track of the results.

I came across this today, which I think is absolutely brilliant...in case you are needing the ID.
If there is something more simple than this, please let me know.

async.each(Object.keys(arr), function(index, cb) {
  console.log('index: ' + index)
  var item = arr[index]
  console.log(item)
  cb()
}, (err, ret) {
  console.log('done')
})
Was this page helpful?
0 / 5 - 0 ratings