If the Microtask Queue has more priority than the Macrotask Queue, why the order of console logs is
scriptStartscriptEndsetTimeoutResponseinstead of
scriptStartscriptEndResponsesetTimeoutgiven that 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?
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). So, while fetch is receiving data from server, setTimeout has time to be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With