Skip to content
Onsite

Run Async Tasks in Series

MediumP2Coding

Implement series(tasks), which runs asynchronous tasks one after another.

tasks is an array of functions. Each takes no arguments and returns a promise or a plain value. series returns a promise that resolves with the array of results, in task order, once the last task has finished.

  • A task starts only after the previous task has finished.
  • If a task rejects (or throws), stop: the returned promise rejects with that error and the remaining tasks are never started.
  • An empty array resolves with [].

Example 1

Input
series([() => delay(20, 'a'), () => delay(5, 'b')])
Output
resolves with ['a', 'b'] after about 25 ms

Constraints

  • 0 <= tasks.length <= 10^3

Hints

Run checks the 3 visible tests, Submit all 8.