Promise实现队列
遍历数组[1,2,3,4,5],每个1秒输出一个数字。
function queue(arr) {
let promise = Promise.resolve();
arr.forEach(val => {
promise = promise.then(() => {
return new Promise(resolve => {
setTimeout(() => {
resolve(val);
}, 1000)
}).then(val => {
console.log(val);
})
})
});
}
queue([1,2,3,4,5])