我正在尝试利用 es7 异步函数,即
async function stepVerifyIdentity(nextState, replace, callback) {
const val1 = await promise1('Param1')
const val2 = await promise2('Param2')
const val3 = await promise3('Param3')
if (!val1 && (!val2 || !val3)) {
console.log('Do something')
}
}
如果 ajax 响应满足传递的参数,这里所有 promise* 函数都会进行 ajax 调用并返回 true
或 false
,我相信我不能使用 3 个等待连续,因此需要一种方法来等待所有这些调用以某种方式返回它们的值。
最佳答案
您可以根据需要多次使用 await
,因此您的示例会执行您想要的操作。
但是,也许你会考虑 Promise.all
更漂亮:
async function stepVerifyIdentity(nextState, replace, callback) {
const [ val1, val2, val3 ] = await Promise.all([
promise1('Param1'),
promise2('Param2'),
promise3('Param3')
])
if (!val1 && (!val2 || !val3)) {
console.log('Do something')
}
}
关于javascript - 在一个异步函数中等待多个 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39371954/