简体   繁体   中英

Why does callback queue runs faster than microtask queue?

So it is said that the microtask queue always runs before the callback queue, right? But what I have is this block of code which runs setTimeout before promise result. Can someone explain why does it work like that?

function display(data) {
  console.log(data);
}

function printHello() {
  console.log("hello");
}

setTimeout(printHello, 0);

const featureData = fetch("https://jsonplaceholder.typicode.com/comments");
featureData.then(display);

console.log("Me First");

In your example the promise result will run after the promise has been resolved. In your case you are calling to an API which will result in some delay.

Your timeout callback will run after a 0 ms delay which is faster than waiting for the API response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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