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.
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 ]
. If I log allBranches
after the loops have finished, the data is all there.
I'm obviously missing a step after the github requests. 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
). So you end up with an array of undefined
, which is what you pass to Promise.all
You need a 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. You're storing them in allBranches
. So you'd need to wait on allBranches
, not 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).
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}))
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.