You have a report that concurrent results are attached to the wrong items. Reproduce it with the smallest distinguishing schedule: two jobs, second completed first. Record the expected input ordering before implementing the fix.
A regression test should remain useful after refactoring. It should assert the public result rather than the names of helper functions or the exact count of internal await statements. Use controlled gates and retain the test after fixing the bug.
In this checkpoint, deliberately compare a correct Promise.all collector with a broken completion-order collector. The demonstration is small enough to understand completely. In the downloaded pool, the same principle applies to indexed assignment in worker loops.
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.
async function main() {
const values = await Promise.all([
Promise.resolve("A"),
Promise.resolve("B")
]);
console.log(values.join(","));
}
main();Check your prediction
A,B Acceptance cases before you code
Write a small test plan before implementing distinguishing regression evidence. These cases extend the brief; they are not a second worked solution. Predict the expected behavior independently, then test through the public function. A good ordinary example and a good boundary example should catch different possible mistakes.
| Case to try | What your result should demonstrate |
|---|---|
| Broken push collector with B released first | Reproduce completion-order output B,A |
| Ordered collector with same schedule | Verify A,B |
| Same expected values chosen independently | Do not compute expected by reusing the broken collector |
After the core task passes, deliberately break one rule and identify the case that notices it. Restore the implementation and explain the failure in terms of the contract, not the number of assertions. If a case requires a concept you have not recalled, revisit the relevant lesson before opening the solution.
Your turn
Checkpoint: Regression Evidence: practise
Build two deferred promises, register a broken collector that pushes on completion, resolve B before A, and assert the result is B,A to reproduce the defect. Then collect the original promises with Promise.all and assert A,B. Print regression reproduced and ordered result verified.
// Keep the same controlled schedule for both observations.Hint 1: the idea
Promise.all retains the ordering of its input promise list.
Hint 2: a first step
Await both registered completion callbacks before inspecting the broken array.
Show one solution
async function main() {
let releaseA;
let releaseB;
const a = new Promise(resolve => {
releaseA = resolve;
});
const b = new Promise(resolve => {
releaseB = resolve;
});
const broken = [];
const observed = [
a.then(value => broken.push(value)),
b.then(value => broken.push(value))
];
releaseB("B");
releaseA("A");
await Promise.all(observed);
if (broken.join(",") !== "B,A") {
throw new Error("Schedule not reproduced");
}
console.log("regression reproduced");
const correct = await Promise.all([a, b]);
if (correct.join(",") !== "A,B") {
throw new Error("Order contract failed");
}
console.log("ordered result verified");
}
main();regression reproduced
ordered result verifiedExplain why the implementation meets the stated contract. Which part is enforced by the language or checker, and which part needs behavioral evidence?
Change it and test it
Write down the actual test you would keep in production: it must reject B,A as a public result, rather than celebrate reproducing it. Run that expectation against the pool. Explain why a sleep-based version is weaker evidence.
Completion is saved in this browser only. It does not measure mastery.