convert async.eachLimit to promise

2019-08-12 13:21发布

问题:

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.