Good API documentation tells callers how to use the library correctly and what it cannot guarantee. Our limit caps active mapper calls from 1 to 64. It does not promise CPU parallelism, constant total memory, a timeout, retries, or interruption of a noncooperating callback.
The rejection boundary is especially important: the pool waits for already active callbacks. If a callback ignores abort and never settles, neither does the pool. A caller needing a deadline must design the work itself to stop or settle, not only race an unrelated timer and pretend the underlying resource disappeared.
Input order determines output positions; first observed failure determines the primary failure. A single phrase such as “fast and safe” does not communicate either rule. Pair each guarantee with a small example and a test location.
For extensions, change the contract before the implementation. Streaming results would need a new ordering and buffering policy. Supporting async iterables would require serializing next requests and waiting for async cleanup. Those are separate APIs, not a one-line promise to support everything.
Predict, run, and explain
Write the output and trace the important state changes before checking it. Then run the program. Each editor is independent. Async examples end with main() so this editor waits for their work.
const guarantees = [
"bounded active calls",
"input-order results",
"cooperative abort"
];
console.log(guarantees.join("; "));Check your prediction
bounded active calls; input-order results; cooperative abortReplace vague promises with observable rules
A useful guarantee names a trigger, outcome and limit. After first observed mapper failure, the pool stops pulling but waits for active callbacks: that sentence specifies both behavior and settlement. Finite input and eventually settling callbacks are assumptions the caller supplies. Bounded concurrency alone promises neither constant result memory nor interruption of uncooperative work.
| Boundary or case | What to inspect |
|---|---|
| Order | Output follows input positions rather than completion |
| Cancellation | Signal requests cooperation and cannot undo side effects |
| Ownership | Items remain shared references and collected results consume growing storage |
Write a counterexample to instant cancellation or constant memory, then state the narrower guarantee this API actually supports. Link it to a distinguishing test. Specify an extension’s new ordering and cleanup policy before promising streaming or asynchronous sources.
Test a claim with a counterexample
Before writing a guarantee, try to break it. Here is the shortest program that breaks the tempting claim that aborting stops the work.
async function main() {
const controller = new AbortController();
const writes = [];
async function save(signal) {
await null; // some async step
writes.push("saved"); // this callback never looks at signal
}
const work = save(controller.signal);
controller.abort("too slow");
await work;
console.log(controller.signal.aborted, writes.join(","));
}
main();Check your prediction
true savedThe signal is aborted and the write still happened. That one run is enough to disprove a README sentence like "aborting stops all work". The accurate sentence is narrower: abort asks callbacks to stop, the pool waits for the ones already running, and only callbacks that check their signal actually stop early. A good guarantee is one that no counterexample like this can break.
Your turn
Write the README paragraph
A caller is about to use mapLimit with slow callbacks that sometimes fail, and with a Cancel button wired to an AbortSignal. Write the paragraph of the README they need, in your own words, in the box below. Aim for four to six sentences. Check it against this list before opening the model answer. Each guarantee should name:
- trigger: what event it is about (a mapper failure, an abort, the source running out);
- outcome: what the caller observes (what the promise rejects with, which results exist, whether new work starts);
- limit: what it does not promise (interrupting a callback that ignores its signal, constant memory, timeouts);
- test location: which test in pool.test.js shows it (use the test names from the downloaded file or the Tests & Failure Lab).
Hint 1: the idea
Write about what a caller can observe, not how the code works inside. "The pool keeps a failed flag" is an implementation detail; "after the first failure no new item is started" is a guarantee the caller can rely on and a test can check.
Hint 2: a first step
Start with the failure case, because it has the most surprising settlement rule. Write one sentence for each of: what happens to new work, what happens to work already running, and when and with what the promise rejects. Then do the same for an abort, and finish with what is not promised.
Show one solution
Model answer
mapLimit(items, mapper, { limit, signal })runs at mostlimitmapper calls at a time and resolves with results in input order, whatever order the calls finish in. When a mapper call fails, no new item is started; calls already running are sent an abort on their signal, and the returned promise waits for every one of them to settle before rejecting with aTaskErrorwhoseindexandcauseidentify the first failure observed (see "first failure stops pulling and waits for active work"). If the caller'ssignalaborts, the same happens, but the promise rejects withsignal.reasonitself, and a signal that is already aborted starts no work at all ("pre-abort starts no jobs; mid-flight abort preserves reason and closes source"). Abort is a request: a callback that ignores its signal keeps running, and if it never settles, neither doesmapLimit. The pool does not add timeouts, and it keeps every result until the end, so memory grows with the input.
Why this answer works. Each sentence has a trigger (a failure, the caller's abort, a pre-aborted signal) and an observable outcome (which promise settles, when, and with what). The limits are stated as plainly as the guarantees, including the one a caller is most likely to get wrong: abort does not interrupt uncooperative work, as the counterexample above shows. The test names point to the evidence, so a reader who doubts a sentence can run it. Compare your paragraph line by line against the checklist. A sentence with no outcome a caller could observe is the one to rewrite.
Change it and test it
Propose one extension, such as a per-call timeout option. Write its README sentence first, using the same checklist, then list the tests you would need before advertising it. Which existing sentence in your paragraph would the extension change?
Completion is saved in this browser only. It does not measure mastery.