2 回答
TA贡献1887条经验 获得超5个赞
您可以返回一个包含Promise.all结果的对象和intermediateData
const unsubscribe = firebase
.locations()
.once('value')
.then(async snapshot => {
const promises = []
const intermediateData = snapshot.val()
snapshot.forEach(element => {
promises.push(firebase.user(element.key).once('value'))
})
return Promise.all(promises).then(snapshots => ({snapshots, intermediateData})
// ^^ return object
})
.then(({snapshots, intermediateData}) => {
// ^^^ destructure returned object
const userInformation = []
snapshots.forEach(userSnapshot => {
if (userSnapshot.exists()) {
userInformation.push({
userId: 1,
name: userSnapshot.val().username
})
}
})
})
TA贡献1821条经验 获得超4个赞
将其放在外封口中:
let intermediateData;
const unsubscribe = firebase
.locations()
.once('value')
.then(async snapshot => {
const promises = []
intermediateData = snapshot.val()
snapshot.forEach(element => {
promises.push(firebase.user(element.key).once('value'))
})
return Promise.all(promises)
})
.then(snapshots => {
//
// Now you can access intermediateData here
//
// ...
})
添加回答
举报