Javascript 多个Promise.all-等待每个(和所有)完成
我正在尝试按顺序执行多个Javascript 多个Promise.all-等待每个(和所有)完成,javascript,firebase-realtime-database,es6-promise,Javascript,Firebase Realtime Database,Es6 Promise,我正在尝试按顺序执行多个Promise.all(),并等待每个Promise.all()完成,然后返回Promise.resolve() 本质上我想 首先-将数据写入临时位置 等待临时写入完成,然后将数据移动到位 等待数据移动到位,然后-清理临时数据 等待以上所有操作完成,然后再解决问题 我的代码存在于Firebase实时数据库触发器事件中 我遇到的问题是,在所有承诺都得到解决之前,数据库功能完成得太早。为什么功能在所有承诺完成之前完成 return Promise.all(firstPro
Promise.all()
,并等待每个Promise.all()
完成,然后返回Promise.resolve()
本质上我想
return Promise.all(firstPromises)
.then((firstResult) => {
// Proceed move data from firstPromises to another location
let movePromises = []
movePromises = movePromises.concat(myFunc.moveStuff({dbRoot, from:`${tmpPath}/1`, to:'one'}))
movePromises = movePromises.concat(myFunc.moveStuff({dbRoot, from:`${tmpPath}/2`, to:'two'}))
movePromises = movePromises.concat(myFunc.moveStuff({dbRoot, from:`${tmpPath}/3`, to:'three'}))
movePromises = movePromises.concat(myFunc.moveStuff({dbRoot, from:`${tmpPath}/4`, to:'four'}))
return Promise.all(movePromises)
})
.then((moveResult) => {
// Result from Promise.all(movePromises)
// Clean up
return Promise.all(cleanPromises)
})
.then((cleanResult) => {
// Result from Promise.all(cleanPromises)
return Promise.resolve(true)
})
.catch((err) => {
console.error('Error', err)
// Clean up
cleanPromises.push(myFunc.cleanup({dbRoot, path:tmpPath}))
return Promise.all(cleanPromises)
// return Promise.reject(err)
})
myFunc.moveStuff():
这是我的控制台注销:
cleanup my/SYSTEM
moveStuff from '/my-tmp/SYSTEM/1' --> 'one'
moveStuff from '/my-tmp/SYSTEM/2' --> 'two'
moveStuff from '/my-tmp/SYSTEM/3' --> 'three'
moveStuff from '/my-tmp/SYSTEM/4' --> 'four'
Function execution took 3473 ms, finished with status: 'ok'
/my-tmp/SYSTEM/1 to move: 9
/my-tmp/SYSTEM/2 to move: 100
/my-tmp/SYSTEM/3 to move: 0
/my-tmp/SYSTEM/4 to move: 22
我通过使用更多控制台日志和异步/等待的实现来澄清执行过程,从而解决了这个问题
moveStuff()
承诺链:
return Promise.all(firstPromises)
.then((firstResult) => {
// Proceed move data from firstPromises to another location
console.log('First finished')
let movePromises = []
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/1`, to:'one'}))
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/2`, to:'two'}))
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/3`, to:'three'}))
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/4`, to:'four'}))
return Promise.all(movePromises)
})
.then((moveResult) => {
// Result from Promise.all(movePromises)
console.log('Move finished')
// Clean up
return Promise.all(cleanPromises)
})
.then((cleanResult) => {
console.log('Clean finished')
// Result from Promise.all(cleanPromises)
return Promise.resolve(true)
})
.catch((err) => {
console.error('Error', err)
// Clean up
cleanPromises.push(myFunc.cleanup({dbRoot, path:tmpPath}))
return Promise.all(cleanPromises)
// return Promise.reject(err)
})
我的注销现在显示
First finished
Move finished
Clean finished
Function execution took 3473 ms, finished with status: 'ok'
你能在Firebase之外复制这个问题吗?如果你发布了一个。你能在代码中添加一些
console.log
语句(以及它们的输出)来显示出哪里出了问题(这样其中一个打印得太早了),那我就太好了。嗨@FrankvanPuffelen!我想我可能忘了在“moveStuff()”中等待:我已经添加了更多的代码和注销来澄清/KAh,我在错误的位置添加了我的console.log,并且没有异步/等待“moveStuff”-现在它工作了/很高兴听到!如果您认为其他人可能会犯同样的错误,那么发布一个带有工作代码的自我回答和修复的解释可能是值得的。否则,我将投票以“打字错误”结束这篇文章。
return Promise.all(firstPromises)
.then((firstResult) => {
// Proceed move data from firstPromises to another location
console.log('First finished')
let movePromises = []
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/1`, to:'one'}))
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/2`, to:'two'}))
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/3`, to:'three'}))
movePromises = movePromises.concat(await myFunc.moveStuff({dbRoot, from:`${tmpPath}/4`, to:'four'}))
return Promise.all(movePromises)
})
.then((moveResult) => {
// Result from Promise.all(movePromises)
console.log('Move finished')
// Clean up
return Promise.all(cleanPromises)
})
.then((cleanResult) => {
console.log('Clean finished')
// Result from Promise.all(cleanPromises)
return Promise.resolve(true)
})
.catch((err) => {
console.error('Error', err)
// Clean up
cleanPromises.push(myFunc.cleanup({dbRoot, path:tmpPath}))
return Promise.all(cleanPromises)
// return Promise.reject(err)
})
First finished
Move finished
Clean finished
Function execution took 3473 ms, finished with status: 'ok'