Run async tasks in series
intermediate Async/await Challenge 13 of 18
Write runInSeries(tasks). tasks is an array of functions that each return a Promise (or a plain value). Run them one at a time, in order, and resolve with their results.
Requirements
- Do not call a task until the previous task’s Promise has resolved.
- Resolve with an array of results in task order.
- If a task rejects, reject with the same error and do not start any remaining tasks.
- An empty array resolves to
[]. - Tasks that return plain (non-Promise) values work too.
Expected output
await runInSeries([() => delay(30, 'a'), () => delay(10, 'b')]) → ['a', 'b'], and task 2 starts only after task 1 has finished
Test cases (6)
- ○resolves with results in task order
- ○starts each task only after the previous one finished
- ○rejects with the first error
- ○does not start tasks after a failure
- ○an empty list resolves to an empty array
- ○accepts tasks that return plain values
solution.jsExecutes in your browser
Ctrl/⌘ + Enter to run tests · your code is saved in this browser
Results
Run the tests to see results here.