异步/等待NodeJS



我正在使用Node.js,在async/await方面遇到了一些问题。我的项目每天从几个端点重新提取一次数据,并通过调用一个广泛的refetch函数来实现这一点:

async function refetch() {
await refetchOne();
await refetchTwo();
await refetchThree();
await refetchFour();
await refetchFive();
}

现在,我希望它按特定的顺序重新蚀刻(如上所述,1->2->3->4->5(。然而,有时订单没有得到维护(即refetchFive被调用,并在Four或Three完成之前完成。我如何确保下一个refetch函数只在上一个完成后调用?

串行运行异步/等待函数数组的最简单方法是使用。。。属于这将按顺序执行它们,一次执行一个,并等待每个问题得到解决。

const asyncA = async () => {
return 'a'
}
const asyncB = async () => {
return 'b'
}
const asyncC = async () => {
return 'C'
}
const list = [asyncA, asyncB, asyncC]
for (const fn of list) {
await fn() // call function to get returned Promise
}

最新更新