Skip to main content

Editable event-loop - beginner

Multiple Timers

Change delay patterns and declaration order to see which timer gets to the queue first.

partially simulatedcurated scenariodoes not execute real code
Short answer

The key output order is sync -> first -> second. Both timers wait until the script finishes, then run FIFO.

Real-world bug this helps solve

Console output or cleanup runs in an order that does not match the source code order. Timer order matters in tests, retries, and UI scheduling.

Common wrong assumption

Expecting timers to interrupt synchronous code.

Fix direction

Make ordering explicit instead of relying on when a queued callback might happen to run.

Fixed code pattern
Promise.resolve().then(handleMicrotask);
setTimeout(handleTimer, 0);
// Assert/expect the microtask before the timer.
Visual proof

The console lane proves the visible order: sync -> first -> second. The active line and queues show why source order changed.

How to verify in a real app

Add temporary logs around the sync line, promise callback, and timer, then confirm the order without adding arbitrary delays.

Share
Loading editable case...

Related cases

Continue with nearby confusion while the mental model is fresh.

Recommended next

Was this clear?

This stays local for now. It helps you track where the product still needs sharper cases.

Send detail