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();
}
答案 0 :(得分:3)
这将保证您希望的顺序执行顺序。
async function foo() {
const functions = [this.getAsync, this.getAsyncTwo, ...];
for (let func of functions) {
await func();
}
}
答案 1 :(得分:1)
您可以等待Promise.all()
await Promise.all([this.getAsync(), this.getAsyncTwo(), /* etc */])