Open wangjs-jacky opened 1 year ago
方案一:构建 promise
串
const tasks = [sleep, sleep, sleep];
/* 构建 promise 的 .then 串 */
function asyncFn(tasks) {
const [first, ...otherTasks] = tasks;
otherTasks.reduce((pre, cur) => {
return pre.then(() => cur())
}, first());
}
asyncFn(tasks);
方案二: async + await
const tasks = [sleep, sleep, sleep];
/* 基于 async + await 实现 */
async function asyncFn() {
for (let i = 0; i < tasks.length; i++) {
try {
await tasks[i]();
} catch (error) { }
}
}
asyncFn(tasks);
题目:
Promise
串行 不考虑waterfall
|Bail
| 传参...args
等场景测试代码:
预期结果:打印三次
Hello world