12. 宏任务_微任务

执行过程:

每当执行栈为空时,就检查微任务,有则进栈执行,栈空了再去查微任务,

直到没有微任务时,去宏任务中取一个进栈执行,栈空了又开始检查微任务,循环往复,直至全都为空

宏任务:

I/O (click事件、fs.writeFile)

setTimeout

setInterval

setImmediate (nodejs)

requestAnimationFrame

微任务

process.nextTick (nodejs)

Promise.then().catch()

  • console.log(1);
    setTimeout(function () {
        console.log(2);
    }, 0)
    new Promise((resolve, reject) => {
        reject(true);
    
        setTimeout(function () {
            resolve(false);
        }, 1000);
    
        new Promise((resolve, reject) => {
            resolve(false);
        }).then(() => {
            console.log(6);
        }, () => {
            console.log(7);
        });
    }).then(() => {
        console.log(3);
    }).catch(() => {
        console.log(4);
    });
    console.log(5);
    // 1 5  6 4 2
--------小尾巴 ________一个人欣赏-最后一朵颜色的消逝-忠诚于我的是·一颗叫做野的心.决不受人奴役.怒火中生的那一刻·终将结束...
原文地址:https://www.cnblogs.com/tianxiaxuange/p/10201867.html