Eventloop polls every once in a while to see if anything happened. Take turns asking

There are three important stages of the event cycle

  1. timers poll check

NextTick () Is not part of any of the stages of the Eventloop. Rather, it means that the state of the loop does not mediate(). Execute nextTick immediately. Because eventLoop and JS execution belong to different processes/V8 engines, it is not certain which one is executed first, which causes a problem



Copy the code

What is the order in which the following console.log is executed under nodejs

setImmediate(() => {
  console.log('setImmediate1')
  setTimeout(() => {
    console.log('setTimeout1')
  }, 0);
})

setTimeout(() => {
  console.log('setTimeout2')
  setImmediate(() => {
    console.log('setImmediate2')
  });
}, 0)

Copy the code

Answer: setImmediate1 setTimeout2, setTimeout1 setImmediate2

Chrome 2. New Promise (fn) Fn is executed immediately

Settimeout => later (macro task).then() now (micro task).then is asynchronous. Then (fn) fn is immediately executed The first then reject is just the second promise and nexttick are both microtasks async and await are syntactic sugar forms of promise