Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
本机javascript承诺循环_Javascript_Loops_Promise - Fatal编程技术网

本机javascript承诺循环

本机javascript承诺循环,javascript,loops,promise,Javascript,Loops,Promise,这样做的目的是在继续之前将请求循环几次github.repos.getBranch应该先构建一个包含10个元素的对象,然后才能继续并使用结果 github api请求按预期工作,但在以下位置记录的结果:console.log('++',res)返回以下内容:+[undefined,undefined,undefined,undefined,undefined]。 如果我在循环完成后记录所有分支,那么数据都在那里 在github请求之后,我显然错过了一步。我已经用我能想到的很多方法重构了它,但没有

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

github api请求按预期工作,但在以下位置记录的结果:
console.log('++',res)
返回以下内容:
+[undefined,undefined,undefined,undefined,undefined]
。 如果我在循环完成后记录所有分支,那么数据都在那里

在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)
  })
}

您没有从
config.repositories.map
调用返回任何内容(这是一个没有
返回的详细箭头函数)。因此,您将得到一个未定义的
数组,这就是传递给
Promise的内容。所有
都需要
返回

但这不是唯一的问题。您正在将
startGetData
传递到
Promise.all
,但这不是github承诺的存储位置。您正在将它们存储在
所有分支中。因此,您需要等待
allbranchs
,而不是
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(分支=>({repo,branchs}))

您想要什么最终结果?我猜是一个存储库数组,其中每个条目都是一个分支数组?(还要注意,github API的速率是有限的,但我猜要么您没有进行那么多调用,要么您已经将自己列入了白名单。)正如您所说,我想要一个存储库数组,其中每个存储库都是一个分支数组。基本上是将暂存分支与主分支进行比较,以查看哪个分支较新。这是一个方便的工具,没有太多的请求。到目前为止,只有我一个人,我是这里唯一的问题。好的,我已经更新了我的答案,以表明你可以这样做。谢谢<代码>返回承诺。所有(所有分支)
得到我需要的。@TomFirth:啊,只是一个简单的数组。很高兴这有帮助!