Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
12.6. Run a sequence that carries on after a failure
Medium
Topics
This question is in
12. Callbacks, promises and asyncA 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
function runEach(tasks) // returns a Promise of an arraytasks: 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
runEachreturns a promise, and the first task starts during the call.- Each next task starts only after the previous one has settled, fulfilled or rejected, and right away when it has.
- A rejection does not stop the sequence: the next task still runs.
- 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 isundefined. - The entries are in task order, and the promise fulfills after the last task settles.
- The returned promise never rejects.
- 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. - Each task is called exactly once; an empty array fulfills with
[]; thetasksarray is not changed.
Examples
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" },
// ]await runEach([() => { throw new TypeError("no input"); }, () => 7]);
// [{ status: "rejected", reason: TypeError("no input") }, { status: "fulfilled", value: 7 }]await runEach([]); // []Environment
- Plain JavaScript in a browser page; the tests call the global
runEachfromrun-each.js. - The tests' tasks answer on a virtual clock (
setTimeout), so no test waits for real time. Promise.allSettleddoes not help here: it waits for promises that already run together, not one at a time.
Hints
- Where can a
try/catchgo so that one failure is caught but the loop keeps going? - 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…