Search code examples
concurrencypromisebluebird

Is there a way to use Bluebird's Promise.each concurrently?


Bluebird has a nice function called Promise.map that lets you pass in an extra argument for the amount of concurrent operations.

e.g.

yield Promise.map arrayOfThings, coroutine (thing) ->
  newThing = yield thing.operate()
  database.set newThing
, concurrency: 500

However, Promise.map will keep an array of whatever database.set newThing returns in memory for all of arrayOfThings. I'd rather not store all of that in memory as it bogs down my server. Optimally, I would want to replace Promise.map with Promise.each so it doesn't store the returned values in memory. Unfortunately this is super slow because Promise.each is not concurrent.

Is there any way I can change my code to make it work like that?


Solution

  • First of all, at the moment Promise.each doesn't actually not allocate the array. There is an open issue for this - I'm assigned and I'd like to apologize - I'm not in front of a dev box and have been abroad. I'll try to fix this soon.

    Second of all - no. There is no such functionality at the moment. Promise.each was created in order to precisely run things sequentially. A pull request might be entertained and it shouldn't be too hard to implement on top of PromiseArray. We just haven't really seen the use case before.

    Meanwhile you can use Promise.map.