javascript – 如何在维护执行顺序的同时减少异步等待关键字的使用
作者:互联网
async function foo() {
await this.getAsync();
await this.getAsyncTwo();
await this.getAsyncThree();
await this.getAsyncFour();
}
看看foo有多个await调用,有没有办法简单地保持执行顺序?
我希望有可能写出类似的东西
async function foo() {
await
this.getAsync(),
this.getAsyncTwo(),
this.getAsyncThree(),
this.getAsyncFour();
}
要么
async function foo() {
await
this.getAsync()
.this.getAsyncTwo()
.this.getAsyncThree()
.this.getAsyncFour();
}
解决方法:
这将保证您希望的顺序执行顺序.
async function foo() {
const functions = [this.getAsync, this.getAsyncTwo, ...];
for (let func of functions) {
await func();
}
}
标签:javascript,async-await,ecmascript-2017 来源: https://codeday.me/bug/20190622/1265427.html