【问题标题】:Native javascript promise loop本机 javascript 承诺循环
【发布时间】:2017-07-29 08:39:26
【问题描述】:

这样做的目的是在继续之前循环请求几次。 github.repos.getBranch 应该先构建一个包含 10 个元素的对象,然后我才能继续使用结果。

github api 请求按预期工作,但结果记录在:console.log('++', res) 返回以下内容:++ [ undefined, undefined, undefined, undefined, undefined ]。 如果我在循环结束后登录allBranches,数据就在那里。

在 github 请求之后,我显然错过了一步。我已经尽我所能地重构了它,但没有成功。

getAllData: () => {
  let allBranches = []
  let startGetData = config.repositories.map(repo => {
    config.branches.map(branch => {
      return allBranches.push(
        github.repos.getBranch({
          owner: config.organisation,
          repo,
          branch
        })
      )
    })
  })

  return Promise.all(startGetData)
  .then(res => {
    console.log('++', res)
  })
}

【问题讨论】:

  • 你想要什么结果?我猜是一个存储库数组,其中每个条目都是一个分支数组?
  • (另请注意,github API 是有速率限制的,但我猜要么您没有进行那么多调用,要么您已将自己列入白名单。)
  • 正如你所说,我想要一个存储库数组,其中每个存储库都是一个分支数组。基本上是将暂存分支与主分支进行比较,看看哪个更新。这是一个方便的工具,没有很多请求。到目前为止,只有我,我是这里唯一的问题。
  • 好的,我已经更新了我的答案,以展示你这样做的一种方式。

标签: javascript loops promise


【解决方案1】:

您的config.repositories.map 调用没有返回任何内容(这是一个没有return 的详细箭头函数)。所以你最终得到了一个undefined 的数组,这就是你传递给Promise.all 你需要一个return

但这不是唯一的问题。您将startGetData 传递给Promise.all,但这不是存储github 承诺的地方。您将它们存储在allBranches 中。所以你需要等待allBranches,而不是startGetData

我怀疑您的目标是获取包含一系列分支的存储库数组。如果是这样,您将需要多个 Promise.all 调用(在存储库的分支上等待)和一个整体的 Promise.all 调用(等待所有存储库完成)。

如果这是您的目标,那么它的外观是这样的:

getAllData: () => Promise.all(                    // Gather up all results
  config.repositories.map(repo =>                 // Map repos to promises
    Promise.all(config.branches.map(branch =>     // Map branches to promises
      github.repos.getBranch({                    // Promise for branch
        owner: config.organisation,
        repo,
        branch
      })
    )).then(branches => ({repo, branches}))       // Wrap up branch results in...
  )                                               // ...an object identifying...
)                                                 // ...the repo

这给了你一个这样的对象数组的承诺:

[
    {
        repo: /*...repo...*/,
        branches: [
            /*...branch...*/,
            /*...branch...*/,
            /*...branch...*/
        ]
    },
    {
        repo: /*...repo...*/,
        branches: [
            /*...branch...*/,
            /*...branch...*/,
            /*...branch...*/
        ]
    },
    {
        repo: /*...repo...*/,
        branches: [
            /*...branch...*/,
            /*...branch...*/,
            /*...branch...*/
        ]
    }
]

如果你只想要一个纯数组:

[
    [
        /*...branch...*/,
        /*...branch...*/,
        /*...branch...*/
    ]
    [
        /*...branch...*/,
        /*...branch...*/,
        /*...branch...*/
    ],
    [
        /*...branch...*/,
        /*...branch...*/,
        /*...branch...*/
    ]
]

...然后删除最后的then 子句(.then(branches => ({repo, branches})))。

【讨论】:

  • 谢谢! return Promise.all(allBranches) 得到我需要的东西。
  • @TomFirth:啊,只是一个简单的数组。很高兴有帮助!
猜你喜欢
  • 2015-12-18
  • 1970-01-01
  • 2016-10-11
  • 2015-05-05
  • 2017-03-12
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多