Skip to content

Instantly share code, notes, and snippets.

@flxxyz

flxxyz/main.js

Created May 26, 2020
Embed
What would you like to do?
顺序执行promise
const arr = []
Array(5).fill(0).forEach((value, index) => {
arr.push(promiseFunc(index))
})
// queue(arr)
//船新解法
arr.reduce((pro,next)=> pro.then(next), Promise.resolve())
//生成待执行promise函数
function promiseFunc(index) {
return function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(index)
resolve(index)
}, 1000)
})
}
}
async function queue(list) {
let index = 0
while (index >= 0 && index < list.length) {
await list[index]()
index++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment