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

猜你喜欢

转载自www.cnblogs.com/tianxiaxuange/p/10201867.html