Schedule work on the microtask queue
Promise callbacks and setTimeout callbacks do not share a queue. Everything queued by a resolved promise runs as soon as the current synchronous code finishes, and only after that queue is empty does the engine reach for a timer callback. Batching libraries lean on this to collapse many updates into one.
Implement soon(fn). It schedules fn to be called on the microtask queue: after the currently running synchronous code, and before any setTimeout callback, even one that was scheduled earlier with a delay of 0. Callbacks scheduled with several soon calls run in the order they were scheduled, and a soon called from inside a scheduled callback still runs before any pending timer.
What it has to do
- Never call
fnsynchronously. - Call
fnbefore any already-scheduledsetTimeoutcallback. - Run several scheduled callbacks in scheduling order.
- Do not use
setTimeoutto schedule the callback.
Your workspace
Ready to check it?
4 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.