Can a fetch callback run before a setTimeout callback even with 0 delay in JavaScript?
Yes. fetch callbacks are microtasks, which the event loop drains before any macrotask (setTimeout). So even with setTimeout(cb, 0), the fetch callback runs first if both are ready.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in setTimeout, fetch, and Promises With the Event Loop in JavaScript
Macrotask. setTimeout callbacks go to the macrotask queue. The event loop runs them only after the call stack is empty and all microtasks are done. This is why setTimeout runs after promise callbacks.
Microtask. fetch returns a promise. The .then callback goes to the microtask queue. The event loop drains all microtasks before any macrotask, so fetch callbacks run before setTimeout callbacks.
Microtask. Promise.then, .catch, and .finally callbacks go to the microtask queue. The event loop drains all microtasks before any macrotask. This is why promises always run before setTimeout.
Still have questions?
Browse all our FAQs or reach out to our support team
