这道题比较简单的一种做法是可以用Promise配合着reduce不停的在promise后面叠加.then,请看下面的代码:
const arr = [1, 2, 3];
arr.reduce((p, x) => {
return p.then(() => {
return new Promise((r) => {
setTimeout(() => r(console.log(x)), 1000);
});
});
}, Promise.resolve());
还可以更简单一点写:
const arr = [1, 2, 3];
arr.reduce(
(p, x) =>
p.then(() => new Promise((r) => setTimeout(() => r(console.log(x)), 1000))),
Promise.resolve(),
);