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

Is a setTimeout callback a microtask or macrotask in JavaScript?

The answer

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.

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

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.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.