# 事件循环机制

Event Loop

The Event Loop has one simple job — to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, the Event Loop will take the first event from the queue and will push it to the Call Stack, which effectively runs it.

# pre know

// ajax(..) is some arbitrary Ajax function given by a library
var response = ajax('https://example.com/api');
console.log(response);
// `response` won't have the response

---
//A simple way of “waiting” for an asynchronous function to return its result
//is to use a function called callback:
ajax('https://example.com/api', function(response) {
    console.log(response); // `response` is now available
});

# summary

  • js单线程!所谓的多线程异步巴拉巴拉都是单线程模拟出来的
  • 先同步,再异步
  • 有一个执行栈、主线程先去处理同步任务、异步都是队列,当主线程空闲会去异步队列里取事件出来处理
  • 异步里先清空微任务,再清空宏任务,
  • 以上是一个循环

# 分三个部分

  • 主执行栈
  • 宏任务 task 队列
    • run script
    • setTimeout
    • setInterval
    • i/o
    • ui render
  • 微任务 micro-task 队列
    • Promise.then/catch
    • process.nextTick
    • await/async
    • Mutation Observer( a interface provides the ability to watch for changes being made to the DOM tree.)
Last Updated: 2022/6/26 上午11:43:14