Skip to main content

Demo 44

Fake timers vs promises

Advancing fake timers does not automatically flush every Promise microtask.

fully simulatedcurated scenariodoes not execute real code
Short answer

The key output order is timer -> promise. Timers and microtasks are different queues, and test helpers may advance only one of them.

Real-world bug this helps solve

A production symptom appears first: flaky tests, stale state, slow UI, late cleanup, or growing memory. This is common in Jest/Vitest tests for debounced inputs, retries, loading spinners, and async React components.

Common wrong assumption

Writing flaky tests that assume all async work finished after advancing timers.

Fix direction

Fix the root scheduling, cleanup, or concurrency pattern instead of adding arbitrary waits.

Fixed code pattern
try {
  await operation();
} finally {
  cleanup();
}
Visual proof

The console lane proves the visible order: timer -> promise. The active line and queues show why source order changed.

How to verify in a real app

Reproduce the symptom with a small test, apply the safer pattern, then verify the flaky, slow, or leaking behavior disappears.

Share
Loading visual timeline...

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