简体   繁体   English

微任务队列是否比宏任务队列具有更高的优先级?

[英]Does the Microtask Queue have more priority than the Macrotask Queue?

If the Microtask Queue has more priority than the Macrotask Queue, why the order of console logs is如果微任务队列的优先级高于宏任务队列,为什么控制台日志的顺序是

  • scriptStart
  • scriptEnd
  • setTimeout
  • Response

instead of代替

  • scriptStart
  • scriptEnd
  • Response
  • setTimeout

given that for(let i=0;i<100000000;i++){ const start = Date.now(); }鉴于for(let i=0;i<100000000;i++){ const start = Date.now(); } for(let i=0;i<100000000;i++){ const start = Date.now(); } takes enough time to keep the main thread busy, until the response from fetch arrives? for(let i=0;i<100000000;i++){ const start = Date.now(); }需要足够的时间,以保持主线程繁忙,直到响应从取到来?

Full Code完整代码

 console.log("scriptStart") setTimeout(() => { console.log("setTimeout"); }, 0); fetch("https://jsonplaceholder.typicode.com/todos/1").then(() => { console.log('Response'); }); for (let i = 0; i < 100000000; i++) { const start = Date.now(); } console.log("scriptEnd")

As you can see in Network tab of debugger, the request to server starts only when thread was released (for loop ended).正如您在调试器的 Network 选项卡中看到的那样,只有在线程被释放(for 循环结束)时才会启动对服务器的请求。 So, while fetch is receiving data from server, setTimeout has time to be done.因此,当fetch从服务器接收数据时, setTimeout有时间完成。

Because the interpreter is singlethreaded.因为解释器是单线程的。 It looks like you are mistaking tasks for threads.看起来您将任务误认为是线程。 They are not.他们不是。 They are just a linked list of callbacks.它们只是回调的链表。

The interpreter works like this:解释器的工作方式如下:

    START
      │
      ▼
    execute
    javascript
      │
      ▼
    Event loop
    ┌──────────┐
    │          │
    │          │
    │          ▼
    │        check if there's any  ─────────▶ handle event
    │        event completion                       │
    │          │                                    │
    │          ▼                                    │
    │        check if we need to execute  ◀─────────┘
    │        any javascript in ───────────────▶ execute
    │        the microtask list                javascript
    │          │                                    │
    │          ▼                                    │
    │        check if we need to execute  ◀─────────┘
    │        any javascript in ───────────────▶ execute
    │        the macrotask list                javascript
    │          │                                    │
    │          │                                    │
    └────◀─────┴─────────────────◀──────────────────┘

It's all loops.都是循环。 Nothing runs in parallel.没有任何东西可以并行运行。 So as long as you are executing javascript you are not entering the event loop.因此,只要您正在执行 javascript,您就不会进入事件循环。

Note: If you are using worker_threads or webworkers then that's a different story because they really spawn threads.注意:如果您使用的是 worker_threads 或 webworkers,那就是另一回事了,因为它们确实会产生线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM