Facebook Pixel

In what order do sync code, promises, and setTimeout run in JavaScript?

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.

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

Want to upskill yourself?

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.