async abc(){
await some().then(() => {
//do inside then
});
//other code
}
“ await”是只在some()上等待还是在进入//其他代码之前等待some()及其then()?基本上,问题是在转移到下一个语句之前,等待的部分还要等待。
答案 0 :(得分:6)
some().then()
返回一个新的promise,await
等待该新的promise,因此它将等待.then()
处理程序和它可能返回的所有promise继续经过{{ 1}}。换句话说,它等待整个承诺链。
通常,在同一条语句中混合使用await
和await
并不是一种很好的风格,因为您通常希望坚持使用.then()
而不是await
:
.then()
这使async abc(){
let result = await some();
// do something with result
//other code
}
支持的外观更简单,顺序更连续。
答案 1 :(得分:-1)
您还可以使用箭头功能来实现这一目标。
const some = () => 'something';
const abs = async () => {
const result_from_some = await some();
// do something with result_from_some
}
但是,如果' result_from_some '中的值可能发生变化,那么最好使用 let 代替 const
希望这对某人有帮助。