Open EdwardZZZ opened 6 years ago
[...Array(5)].forEach((_, i) => {
excute(i);
});
function excute(n) {
this.prev = this.prev ? this.prev.then(() => print(n)) : print(n);
/*
clearTimeout(this.timeout);
this.taskList = this.taskList || [];
this.taskList.push(n);
this.timeout = setTimeout(async () => {
for(const task of this.taskList) {
await print(task)
}
}, 0);
*/
}
async function print(n) {
await new Promise(resolve => setTimeout(resolve, Math.random() * 1e3));
console.log(n);
}
可以看做是简版的waterfall实现,和Promise.all乱序不同,这个是顺序执行