Delay Queue

MediumUtility Functions20 min4 tests

Implement a delayQueue function that executes an array of functions sequentially with a fixed delay between each execution, and returns a promise that resolves when all functions have been called.

Requirements

  • Accept an array of functions and a delay in milliseconds
  • Execute each function one at a time, waiting the specified delay between each
  • Return a promise that resolves with an array of return values
  • The first function should execute immediately (no initial delay)

Example

const results = await delayQueue([() => 1, () => 2, () => 3], 100);
// results: [1, 2, 3] (each called 100ms apart)
Hints (4)
  1. Return a new Promise so the caller can await completion.
  2. Use a recursive helper function or a loop with setTimeout.
  3. Execute the first function immediately, then wait before each subsequent one.
  4. Collect each function's return value in an array and resolve with it at the end.

Topics

  • Utility Functions

Asked at

Google · Meta · Uber · Airbnb

Join Us
blur