Scriptdrill

Level 5: Async and functions Exercise 45 of 45 Pro

Run tasks in sequence

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

`tasks` is an array of functions; each returns a Promise or a plain value. Run them one at a time, in order, starting each only after the previous one has finished, and resolve with the array of their results. If a task fails, reject and do not start the remaining tasks. Tests run on a fake clock: `setTimeout`, `clearTimeout` and `Date.now` are simulated, so waits are instant and exact.

What this exercise teaches

await inside a for...of loop pauses the loop until each task is done, so tasks never overlap and the total time is the sum of their times. A rejection throws out of the loop before the next task starts.

A hint if you get stuck

In an `async` function, loop with `for...of` and `await task()` inside the loop. `Promise.all(tasks.map(...))` would start them all at once.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Async and functions