Skip to curriculum
Jump to the editor

12.6. Run a sequence that carries on after a failure

Medium
Topics
15–25 min

A nightly job sends reminder emails, one user at a time so the mail server is not flooded. If one address bounces, the others must still get their email, and at the end the job needs a report: which sends worked, and why the others failed. A plain sequence stops at the first failure; this one carries on and records every outcome, in order.

Write runEach(tasks) in run-each.js. It runs async tasks one at a time and fulfills with a report entry per task, in the same shape Promise.allSettled uses.

Signature

JAVASCRIPT
function runEach(tasks) // returns a Promise of an array
  • tasks: an array of functions. Each returns a promise, or a plain value.
  • The promise fulfills with one entry per task: { status: "fulfilled", value } or { status: "rejected", reason }.

Requirements

  1. runEach returns a promise, and the first task starts during the call.
  2. Each next task starts only after the previous one has settled, fulfilled or rejected, and right away when it has.
  3. A rejection does not stop the sequence: the next task still runs.
  4. A fulfilled task gives { status: "fulfilled", value }; a rejected one gives { status: "rejected", reason } with the very same reason. Each entry has exactly those two keys, even when the value or reason is undefined.
  5. The entries are in task order, and the promise fulfills after the last task settles.
  6. The returned promise never rejects.
  7. A task that throws instead of returning a promise counts as rejected with what it threw. An item that is not a function counts as rejected with a TypeError. A task that returns a plain value counts as fulfilled with it.
  8. Each task is called exactly once; an empty array fulfills with []; the tasks array is not changed.

Examples

JAVASCRIPT
const send = (address, ms) => () =>
  new Promise((resolve, reject) => setTimeout(() =>
    address.includes("@") ? resolve(`sent to ${address}`) : reject(new Error(`bad address ${address}`)), ms));

await runEach([send("ana@example.com", 100), send("nobody", 50), send("li@example.com", 100)]);
// after 250 ms:
// [
//   { status: "fulfilled", value: "sent to ana@example.com" },
//   { status: "rejected", reason: Error("bad address nobody") },
//   { status: "fulfilled", value: "sent to li@example.com" },
// ]
JAVASCRIPT
await runEach([() => { throw new TypeError("no input"); }, () => 7]);
// [{ status: "rejected", reason: TypeError("no input") }, { status: "fulfilled", value: 7 }]
JAVASCRIPT
await runEach([]); // []

Environment

  • Plain JavaScript in a browser page; the tests call the global runEach from run-each.js.
  • The tests' tasks answer on a virtual clock (setTimeout), so no test waits for real time.
  • Promise.allSettled does not help here: it waits for promises that already run together, not one at a time.

Hints

  1. Where can a try/catch go so that one failure is caught but the loop keeps going?
  2. Calling a task can throw before any promise exists. Is that call inside your try?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…