4
async function foo() {
  await this.getAsync();
  await this.getAsyncTwo();
  await this.getAsyncThree();
  await this.getAsyncFour();
}

看看 foo 如何有多个等待调用,有没有办法在保持执行顺序的同时简化它?

我希望有可能写出类似的东西

async function foo() {
  await 
   this.getAsync(), 
   this.getAsyncTwo(), 
   this.getAsyncThree(), 
   this.getAsyncFour();
}

或者

async function foo() {
  await 
   this.getAsync() 
   .this.getAsyncTwo() 
   .this.getAsyncThree()
   .this.getAsyncFour();
}
4

2 回答 2

3

这将保证您希望的顺序执行顺序。


async function foo() {
  const functions = [this.getAsync, this.getAsyncTwo, ...];

  for (let func of functions) { 
    await func();
  }
}
于 2017-04-13T11:35:37.570 回答
1

You can await on a Promise.all()

await Promise.all([this.getAsync(), this.getAsyncTwo(), /* etc */])
于 2017-04-13T11:30:55.420 回答