Facebook Pixel

setTimeout, fetch, and Promises With the Event Loop in JavaScript

Three common async APIs and how they interact with the event loop. Here is the timing for each.

setTimeout, fetch, and Promises With the Event Loop in JavaScript

setTimeout, fetch, and promises are the three most common async APIs. Here is how each interacts with the event loop.

setTimeout

setTimeout(() => console.log("timer"), 1000); `` 1. The Web API starts a timer for 1000ms. 2. The engine continues. 3. When the timer fires, the callback goes to the **macrotask queue**. 4. The event loop runs it when the stack is empty and microtasks are done. `setTimeout` callbacks are macrotasks. They run after all microtasks. ### `fetch` ```js fetch("/api").then((data) => console.log("data")); `` 1. The Web API starts a network request. 2. The engine continues. 3. When the response arrives, the `.then` callback goes to the **microtask queue**. 4. The event loop runs it when the stack is empty (before any macrotask). `fetch` callbacks (via promises) are microtasks. They run before macrotasks. ### Promises ```js Promise.resolve().then(() => console.log("promise")); `` 1. The promise resolves immediately. 2. The `.then` callback goes to the **microtask queue**. 3. The event loop runs it when the stack is empty (before any macrotask). Promise callbacks are microtasks. They always run before macrotasks. ### Combined Example ```js setTimeout(() => console.log("timeout"), 0); fetch("/api").then(() => console.log("fetch")); Promise.resolve().then(() => console.log("promise")); console.log("sync"); `` **Output** (assuming fetch resolves quickly): `sync, promise, fetch, timeout` (or `sync, promise, timeout, fetch` if fetch is slow). - `sync` runs first (stack). - `promise` runs next (microtask, already resolved). - `fetch` runs when the response arrives (microtask). - `timeout` runs last (macrotask). ### Key Takeaways - `setTimeout` → macrotask (runs after microtasks). - `fetch` → microtask (via promise, runs before macrotasks). - `Promise.then` → microtask (runs before macrotasks). - Sync code → runs on the stack (first). - Blocking the stack delays everything. ### The Takeaway `setTimeout` callbacks are macrotasks (run after microtasks). `fetch` and `Promise.then` callbacks are microtasks (run before macrotasks). Sync code runs first on the stack. Understanding which queue each API uses explains all async timing.

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.

Sync code runs first on the call stack. Then all microtasks (promise callbacks, fetch .then). Then one macrotask (setTimeout callback). Then microtasks again. This repeats.

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.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.