使用Promise.all()方法和async/await语法糖来替代长长的then()链。
代码示例:
Promise.all([
promise1(),
promise2(),
promise3(),
]).then(([result1, result2, result3]) => {
// do something
}).catch((err) => {
// handle error
});
// 使用async/await语法糖
async function myAsyncFunction() {
try {
const [result1, result2, result3] = await Promise.all([
promise1(),
promise2(),
promise3(),
]);
// do something
} catch (err) {
// handle error
}
}