SOURCE

function p(flag, key) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (flag) resolve(key)
            else reject(key)
        }, 2000)
    })
}

const arr = []
for (let i = 1; i <= 5; i++) {
    arr.push(() => p(i % 2 === 0, i))
}


async function loop(arr) {
    try {
        const curP = arr.shift()
        const res = await curP()
        console.log('res:', res)
    } catch(err) {
        console.log('err:', err)
    } finally {
        if (!arr.length) return
        loop(arr)
    }
}

loop(arr)

console 命令行工具 X clear

                    
>
console