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

陌路散爱 提交于 2019-12-24 01:19:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40408086/is-there-a-way-to-use-bluebirds-promise-each-concurrently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!