AniUI Academy
medium+250 XPPractice

An async queue that preserves order

Autosave is the classic case: every keystroke can enqueue a save, the saves must not overlap or arrive out of order, and one failure must not wedge the queue for the rest of the session.

Implement a class AsyncQueue. push(task) takes a zero-argument function returning a promise, appends it to the queue, and returns a promise that settles with that task's own result. Tasks run strictly one at a time, in the order they were pushed, including tasks pushed while an earlier one is still running. If a task rejects, only the promise returned by its own push call rejects — the queue carries on with the next task.

What it has to do

  • Never run two tasks at the same time.
  • Start tasks in the order they were pushed, including ones pushed mid-run.
  • Resolve each push with that task's own resolved value.
  • Reject only the failing task's promise and keep processing the rest.

Your workspace

Try it yourself
Loading playground...

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.