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 docsMore JavaScript Fundamentals flashcards
- Which keyword declares a variable whose binding cannot be reassigned?
- Which of the following is a primitive data type in JavaScript?
- What is the difference between == and ===?
- What is the main syntactic feature of an arrow function?
- What does a closure keep access to after its outer function has returned?
