SOURCE

function d(taskList, maxCount = 2) {

    // 当前执行的任务数量
    let currentCount = 0

    let currentIndex = 0


    function run() {
        console.log(currentIndex, '<----currentIndex')
        const task = taskList[currentIndex]
        task().then((res) => {
            console.log(res)
            if (currentIndex < taskList.length - 1) {
                currentIndex++
            }
            run()
        })
    }

    for (let i = 0; i < taskList.length && currentCount < maxCount; i++) {
        currentCount++
        currentIndex++
        run()
    }

}


const task = [
    () => Promise.resolve(1),
    () => Promise.resolve(2),
    () => Promise.resolve(3),
    () => Promise.resolve(4),
    () => Promise.resolve(5),
    () => Promise.resolve(6),
    () => Promise.resolve(7),
    () => Promise.resolve(8)
]

d(task)
console 命令行工具 X clear

                    
>
console