事件循环~

91 阅读1分钟
// 同步任务,异步任务进入不同的执行地方, 同步进入主线程,异步进入Event Table并注册函数
    // 异步又会分为
    console.log('1');  // 1  7   8 2 4  5 9 11  12

    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')
      })
    })