간단한 wait 함수를 만들도록 하겠습니다.
const wait = (time) =>
new Promise((resolve) => {
setTimeout(() => {
resolve();
}, time);
});const func1 = async () => {
await wait(1000);
console.log("1 done");
};
const func2 = async () => {
await wait(1000);
console.log("2 done");
};
func1().then(func2)1 done
2 donereduce함수를 이용하여 해당작업은 간편하게 할 수 있습니다.
[func1, func2].reduce((p, f) => p.then(f), Promise.resolve());const run = async () => {
await func1();
await func2();
};
run();for of 사용
const run = async () => {
for (const func of promises) {
await func();
}
};
run();recude 사용
promises.reduce(async (prev, current) => {
await prev;
return current();
}, Promise.resolve());