【JS】Event Loop 宏任务/微任务

写出执行顺序

console.log('[]', 1)
setTimeout(function () {
    console.log('[]', 14)
}, 0)
setTimeout(function () {
    console.log('[]', 2)
    new Promise(function (resolve) {
        resolve()
    }).then(function () {
        console.log('[]', 12)
        setTimeout(function () {
            console.log('[]', 15)
        }, 0)
        new Promise(function (resolve) {
            resolve()
        }).then(function () {
            console.log('[]', 13)
        })
    })
}, 0)
new Promise(function (resolve) {
    console.log('[]', 3)
    for (var i = 0; i < 10000; i++) {
        i === 9999 && resolve()
    }
    console.log('[]', 4)
}).then(function () {
    console.log('[]', 5)
})
setTimeout(function () {
    console.log('[]', 7)
}, 0)
new Promise(function (resolve) {
    console.log('[]', 8)
    resolve()
}).then(function () {
    console.log('[]', 9)
})
console.log('[]', 6)
new Promise(function (resolve) {
    resolve()
}).then(function () {
    console.log('[]', 10)
})
new Promise(function (resolve) {
    resolve()
}).then(function () {
    console.log('[]', 11)
})

结果

1-3-4-8-6-5-9-10-11-14-2-12-13-7-15

你可能感兴趣的:(前端,javascript)