I'm having trouble finding the answer to this. When we call fetch(), is the browser opening a new thread or is the call just put in the microqueue?
CodePudding user response:
The answer is sort of "both":
The network request itself is processed in parallel with anything else happening on your JavaScript thread, not on the JavaScript thread. (In all major browsers.)
When the network request is complete, a promise reaction (fulfillment or rejection) is queued to be picked up by the JavaScript thread when it's next checking the relevant queue. (I think what happens is that the browser queues a task that settles the promise, which in turn queues any relevant promise reactions in the microtask queue. The details don't really matter, though; fundamentally, once the network request is complete, a task/microtask is queued somewhere for any promise reactions that need to happen, and picked up by the JavaScript thread like other tasks/microtasks.)