[英]Native javascript promise loop
The purpose of this is to loop through a request a few times before proceeding. 这样做的目的是在继续进行之前,遍历一个请求几次。 github.repos.getBranch
should build an object with 10 elements before I can continue and use the results. github.repos.getBranch
应该建立一个包含10个元素的对象,然后我才能继续使用结果。
The github api request works as expected, but the results logged at: console.log('++', res)
come back with the following: ++ [ undefined, undefined, undefined, undefined, undefined ]
. github api请求按预期方式工作,但记录在以下位置的结果: console.log('++', res)
返回以下内容: ++ [ undefined, undefined, undefined, undefined, undefined ]
。 If I log allBranches
after the loops have finished, the data is all there. 如果我在循环完成后记录了allBranches
,那么数据就在那里。
I'm obviously missing a step after the github requests. 在github请求之后,我显然缺少了一步。 I have refactored this as many ways as I can think of, with no success. 我已经想到了许多重构方法,但是都没有成功。
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)
})
}
You're not returning anything from your config.repositories.map
call (it's a verbose arrow function with no return
). 您不会从config.repositories.map
调用中返回任何内容(这是一个冗长的箭头函数,没有return
)。 So you end up with an array of undefined
, which is what you pass to Promise.all
You need a return
. 因此,您最终得到一个undefined
的数组,这是您传递给Promise.all
的内容。 Promise.all
需要return
。
But that's not the only issue. 但这不是唯一的问题。 You're passing startGetData
into Promise.all
, but that's not where the github promises are stored. 您正在将startGetData
传递到Promise.all
,但这不是github promises的存储位置。 You're storing them in allBranches
. 您将它们存储在allBranches
。 So you'd need to wait on allBranches
, not startGetData
. 因此,您需要等待allBranches
而不是startGetData
。
I suspect your goal is to get an array of repositories containing an array of branches. 我怀疑您的目标是获得包含分支机构数组的存储库数组。 If so, you'll need multiple Promise.all
calls (waiting on the branches of a repository) and an overall Promise.all
call (waiting for all the repositories to finish). 如果是这样,您将需要多个Promise.all
调用(在存储库的分支上等待)和整个Promise.all
调用(等待所有存储库完成)。
If that's your goal, here's one way it would look: 如果这是您的目标,那么它将是一种看起来:
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
That gives you a promise for an array of objects like this: 这使您可以保证像这样的对象数组:
[
{
repo: /*...repo...*/,
branches: [
/*...branch...*/,
/*...branch...*/,
/*...branch...*/
]
},
{
repo: /*...repo...*/,
branches: [
/*...branch...*/,
/*...branch...*/,
/*...branch...*/
]
},
{
repo: /*...repo...*/,
branches: [
/*...branch...*/,
/*...branch...*/,
/*...branch...*/
]
}
]
If you just want a pure array of arrays: 如果只需要一个纯数组数组:
[
[
/*...branch...*/,
/*...branch...*/,
/*...branch...*/
]
[
/*...branch...*/,
/*...branch...*/,
/*...branch...*/
],
[
/*...branch...*/,
/*...branch...*/,
/*...branch...*/
]
]
...then just remove the final then
clause ( .then(branches => ({repo, branches}))
). ...然后只需删除最后的then
子句( .then(branches => ({repo, branches}))
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.