convert async.eachLimit to promise

南楼画角 提交于 2019-12-24 17:31:41

问题


I have such aync code

async.eachLimit(teams, 1000, fetchTeamInfo, exit)

I need to convert it to Promise (bluebird)

I thought will be good to make something like:

Promise.method (teams, 1000, fetchTeamInfo) ->
  async.parallelLimit arr.map((a, i) ->
    ->
      iterator a, i, arr
  ), limit

But not sure is it right way


回答1:


Well, I see you're using Promise.method so I'm assuming bluebird - bluebird comes with Promise.map with already supports what you're looking for with the concurrency parameter:

const fn = (teams, concurrency) => Promise.map(teams, fetchTeamInfo, {concurrency});

As you see, we didn't really do here much - we can just use Promise.map directly :)

In CoffeeScript I think it'd look something like:

fn = (teams, concurrency) -> Promise.map teams, fetchTeamInfo, concurrency: concurrency

But I haven't written CoffeeScript in almost 3 years.



来源:https://stackoverflow.com/questions/36081160/convert-async-eachlimit-to-promise

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