GnoseedGnoseed
Programming · Flashcard

Which runs first: Promise.resolve().then(f) or setTimeout(f, 0)?

Why this is the answer

Promise.resolve().then(f) queues a microtask, and the microtask queue always drains before the next macrotask — setTimeout(f, 0) schedules a macrotask, so it runs after. The order isn't random; it follows the spec's queue priority deterministically. And the two aren't the same kind of task — timers and microtasks are separate queues entirely.

Read more in the docs
Study in Gnoseed →