【问题标题】:convert async.eachLimit to promise将 async.eachLimit 转换为 Promise
【发布时间】:2016-07-05 00:25:24
【问题描述】:

我有这样的异步代码

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

我需要将其转换为 Promise (bluebird)

我认为制作这样的东西会很好:

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

但不确定是否正确

【问题讨论】:

    标签: javascript node.js asynchronous coffeescript promise


    【解决方案1】:

    好吧,我看到您正在使用 Promise.method,所以我假设 bluebird - bluebird 附带 Promise.map 已经支持您正在寻找的并发参数:

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

    如您所见,我们在这里并没有真正做太多 - 我们可以直接使用Promise.map :)

    在 CoffeeScript 中,我认为它看起来像:

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

    但是我已经快 3 年没有写过 CoffeeScript 了。

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2016-12-25
      • 2018-02-19
      相关资源
      最近更新 更多