Assemble two independent facts
Write async overview(loadProfile, loadCount). Start both operations before awaiting their results. Return a string such as "Ada: 3 books". loadProfile fulfills with a valid record containing name; loadCount fulfills with a valid number. If either rejects, let overview reject rather than substituting a fake profile or zero.
This contract assumes domain-valid results so you can focus on coordination. Write success, zero-count, and rejection checks. Then consider a different dashboard that should display whichever part succeeded. That is a different product requirement and may need a different return shape, not an invisible catch.
Use a controlled local loader for each fact. Do not call an external service or add a sleep. Explain why starting both operations before await overlaps waiting but does not move CPU-bound JavaScript to another thread.
Predict and trace
Write the output before running. Follow the values through each call; if your prediction differs, identify the first step that surprised you.
async function main() {
const values = await Promise.all([
Promise.resolve("Ada"),
Promise.resolve(0)
]);
console.log(values[1]);
}
main();Check your prediction
0Input order determines result positions.
Acceptance cases before you code
Write the expected result for each case before coding. The last case is the one that checks "start both before awaiting": results alone cannot, because a sequential version returns the same strings. Use a deferred promise, as in Promise Coordination, so the profile is still loading when you look.
| Case to try | What your result should demonstrate |
|---|---|
| Both loaders fulfill | Return the documented combined shape |
| A loader fulfills with count zero | Display 0 books rather than substituting a missing-data fallback |
| One loader rejects | Preserve the intended rejection |
| The profile loader returns a deferred promise that is still pending | loadCount has already been called; a version that awaits the profile first fails this case |
When everything passes, rewrite overview with two sequential awaits and confirm that only the overlap case fails. Restore it, then explain in one sentence what that case proves.
Build it yourself
Checkpoint: Async Overview: practice
Implement overview and check all four cases from the table. Print results in this order:
Ada: 3 books
Ada: 0 books
Count unavailable
Count started while profile loading: true
Lin: 2 booksasync function main() {
async function overview(loadProfile, loadCount) {
// Call both dependencies before awaiting their combined result.
}
console.log(await overview(async () => ({ name: "Ada" }), async () => 3));
}
main();Hint 1: the idea
Promise.all matches the all-or-nothing contract, but only if both promises exist before you wait. For the overlap case, the test needs a profile that stays pending until the test releases it.
Hint 2: a first step
For the overlap case, copy the deferred helper from Promise Coordination. Pass () => profileGate.promise as the profile loader and a count loader that sets countStarted = true. Start overview without awaiting it, and print countStarted after one await Promise.resolve().
Show one solution
async function main() {
async function overview(loadProfile, loadCount) {
const profileTask = loadProfile();
const countTask = loadCount();
const [profile, count] = await Promise.all([profileTask, countTask]);
return `${profile.name}: ${count} books`;
}
function deferred() {
let resolve;
const promise = new Promise(done => {
resolve = done;
});
return { promise, resolve };
}
console.log(await overview(async () => ({ name: "Ada" }), async () => 3));
console.log(await overview(async () => ({ name: "Ada" }), async () => 0));
try {
await overview(
async () => ({ name: "Ada" }),
async () => {
throw new Error("Count unavailable");
}
);
} catch (error) {
console.log(error.message);
}
// Overlap: the count must start while the profile is still loading.
const profileGate = deferred();
let countStarted = false;
const pending = overview(
() => profileGate.promise,
async () => {
countStarted = true;
return 2;
}
);
await Promise.resolve();
console.log(`Count started while profile loading: ${countStarted}`);
profileGate.resolve({ name: "Lin" });
console.log(await pending);
}
main();Ada: 3 books
Ada: 0 books
Count unavailable
Count started while profile loading: true
Lin: 2 booksoverview calls both loaders on its first two lines, before any await, so the count is already running while the profile gate is closed. Written as await loadProfile(); await loadCount();, every other case still passes but the overlap line prints false: that case is the only one that checks the stated requirement. Promise.all rejects with the count's error unchanged, and the zero case proves a real 0 is not treated as missing. The loaders use async () => ({ name: "Ada" }): the parentheses make the braces an object to return, as explained in Async & Await.
Test a changed requirement
Specify a partial-success version for a dashboard that shows whichever part loaded. Decide its return shape so a failed count stays visible instead of turning into a plausible zero.
Explain before moving on
What happens to the other operation when one rejects? Why is a rejected overview not a rollback?
Completion is saved in this browser only. It does not measure mastery.