gpt4 book ai didi

javascript - 如何在保持执行顺序的同时减少在异步等待中使用 await 关键字

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:41:46 26 4
gpt4 key购买 nike

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 - 如何在保持执行顺序的同时减少在异步等待中使用 await 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43390968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com