• JavaScript 执行机制


    弄懂 JavaScript 执行机制

    • macro-task(宏任务):包括整体代码script,setTimeout,setInterval
    • micro-task(微任务):Promise,process.nextTick

    不同类型的任务会进入对应的Event Queue,比如setTimeoutsetInterval会进入相同的Event Queue。

    事件循环的顺序,决定js代码的执行顺序。进入整体代码(宏任务)后,开始第一次循环。接着执行所有的微任务。然后再次从宏任务开始,找到其中一个任务队列执行完毕,再执行所有的微任务。听起来有点绕,我们用文章最开始的一段代码说明:

    setTimeout(function() {
        console.log('setTimeout');
    })
    
    new Promise(function(resolve) {
        console.log('promise');
    }).then(function() {
        console.log('then');
    })
    
    console.log('console');
    • 这段代码作为宏任务,进入主线程。

    • 先遇到setTimeout,那么将其回调函数注册后分发到宏任务Event Queue。(注册过程与上同,下文不再描述)。
    • 接下来遇到了Promisenew Promise立即执行,then函数分发到微任务Event Queue。
    • 遇到console.log(),立即执行。
    • 好啦,整体代码script作为第一个宏任务执行结束,看看有哪些微任务?我们发现了then在微任务Event Queue里面,执行。
    • ok,第一轮事件循环结束了,我们开始第二轮循环,当然要从宏任务Event Queue开始。我们发现了宏任务Event Queue中setTimeout对应的回调函数,立即执行。
    • 结束。

    事件循环,宏任务,微任务的关系如图所示:

    我们来分析一段较复杂的代码,看看你是否真的掌握了js的执行机制:

    console.log('1');
    
    setTimeout(function() {
        console.log('2');
        process.nextTick(function() {
            console.log('3');
        })
        new Promise(function(resolve) {
            console.log('4');
            resolve();
        }).then(function() {
            console.log('5')
        })
    })
    process.nextTick(function() {
        console.log('6');
    })
    new Promise(function(resolve) {
        console.log('7');
        resolve();
    }).then(function() {
        console.log('8')
    })
    
    setTimeout(function() {
        console.log('9');
        process.nextTick(function() {
            console.log('10');
        })
        new Promise(function(resolve) {
            console.log('11');
            resolve();
        }).then(function() {
            console.log('12')
        })
    })
    

      第一轮事件循环流程分析如下:

    • 整体script作为第一个宏任务进入主线程,遇到console.log,输出1。
    • 遇到setTimeout,其回调函数被分发到宏任务Event Queue中。我们暂且记为setTimeout1
    • 遇到process.nextTick(),其回调函数被分发到微任务Event Queue中。我们记为process1
    • 遇到Promisenew Promise直接执行,输出7。then被分发到微任务Event Queue中。我们记为then1
    • 又遇到了setTimeout,其回调函数被分发到宏任务Event Queue中,我们记为setTimeout2
    宏任务Event Queue 微任务Event Queue
    setTimeout1 process1
    setTimeout2 then1

     

     

     

     

     

    • 上表是第一轮事件循环宏任务结束时各Event Queue的情况,此时已经输出了1和7。
    • 我们发现了process1then1两个微任务。
    • 执行process1,输出6。
    • 执行then1,输出8。

    好了,第一轮事件循环正式结束,这一轮的结果是输出1,7,6,8。那么第二轮时间循环从setTimeout1宏任务开始:

    • 首先输出2。接下来遇到了process.nextTick(),同样将其分发到微任务Event Queue中,记为process2new Promise立即执行输出4,then也分发到微任务Event Queue中,记为then2
    宏任务Event Queue 微任务Event Queue
    setTimeout2 process2
      then2

     

     

     

     

     

    • 第二轮事件循环宏任务结束,我们发现有process2then2两个微任务可以执行。
    • 输出3。
    • 输出5。
    • 第二轮事件循环结束,第二轮输出2,4,3,5。
    • 第三轮事件循环开始,此时只剩setTimeout2了,执行。
    • 直接输出9。
    • process.nextTick()分发到微任务Event Queue中。记为process3
    • 直接执行new Promise,输出11。
    • then分发到微任务Event Queue中,记为then3
    宏任务Event Queue 微任务Event Queue
      process3
      then3

     

     

     

     

     

    • 第三轮事件循环宏任务执行结束,执行两个微任务process3then3
    • 输出10。
    • 输出12。
    • 第三轮事件循环结束,第三轮输出9,11,10,12。

    整段代码,共进行了三次事件循环,完整的输出为1,7,6,8,2,4,3,5,9,11,10,12。

    原文地址:https://juejin.im/post/59e85eebf265da430d571f89#heading-8

  • 相关阅读:
    初始线程(相关理论)
    python并发编程之多进程2-(数据共享及进程池和回调函数)
    python并发编程之多进程1--(互斥锁与进程间的通信)
    Cpython支持的进程与线程
    网络编程之进程理论简介
    python之网络socket编程
    Gray码 (格雷码) 【二进制】
    [BZOJ 2350] [Poi2011] Party 【Special】
    [BZOJ 1033] [ZJOI2008] 杀蚂蚁antbuster 【模拟!】
    [BZOJ 3209] 花神的数论题 【数位统计】
  • 原文地址:https://www.cnblogs.com/wangtao-/p/10444824.html
Copyright © 2020-2023  润新知