Promise Sequence

EasyPromises & Async10 min5 tests

Implement promiseSequence(fns) that executes an array of async factory functions one at a time, in order.

Requirements

  • fns is an array of functions, each returning a Promise.
  • Execute them sequentially (one at a time).
  • Return an array of results in the same order.

Example

const results = await promiseSequence([
  () => Promise.resolve('a'),
  () => new Promise(r => setTimeout(() => r('b'), 50)),
  () => Promise.resolve('c'),
]);
// ['a', 'b', 'c']
Hints (4)
  1. Use a for...of loop to iterate through the functions.
  2. Await each function call before moving to the next.
  3. Push each result into an array.
  4. Return the completed results array.

Topics

  • Promises & Async

Asked at

Microsoft · Shopify · Atlassian

Join Us
blur