Promise Sequence
EasyPromises & Async10 min5 tests
Implement promiseSequence(fns) that executes an array of async factory functions one at a time, in order.
Requirements
fnsis an array of functions, each returning aPromise.- 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)
- Use a for...of loop to iterate through the functions.
- Await each function call before moving to the next.
- Push each result into an array.
- Return the completed results array.
Topics
- Promises & Async
Asked at
Microsoft · Shopify · Atlassian
