Checkpoint: Async Overview

The Course
•
3 min read

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.

Predict, then run JavaScript
async function main() {
  const values = await Promise.all([
    Promise.resolve("Ada"),
    Promise.resolve(0)
  ]);

  console.log(values[1]);
}

main();
Check your prediction
0

Input order determines result positions.

Acceptance cases before you code

Write a small test plan before implementing async overview outcomes. 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 tryWhat your result should demonstrate
Both loaders fulfillReturn the documented combined shape
One loader rejectsPreserve the intended rejection
Zero-valued successful countKeep it as success instead of a truthy fallback

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.

Build it yourself

Exercise

Checkpoint: Async Overview: practise

Implement overview and print its ordinary result. Then use a rejected count loader and print its caught message.

Your working space JavaScript
// Call both dependencies before awaiting their combined result.
Hint 1: the idea

Promise.all matches the all-or-nothing contract.

Hint 2: a first step

Store each returned promise, then await their array and read its two results.

Show one solution
async function main() {
  async function overview(loadProfile, loadCount) {
    const profileTask = loadProfile();
    const countTask = loadCount();
    const values = await Promise.all([profileTask, countTask]);

    return `${values[0].name}: ${values[1]} books`;
  }

  console.log(
    await overview(
      async () => ({ name: "Ada" }),
      async () => 3
    )
  );

  try {
    await overview(
      async () => ({ name: "Ada" }),
      async () => {
        throw new Error("Count unavailable");
      }
    );
  } catch (error) {
    console.log(error.message);
  }
}

main();
Ada: 3 books
Count unavailable

Test count zero. Then specify a partial-success version that retains error information rather than silently returning a plausible complete overview.

Test a changed requirement

Test count zero. Then specify a partial-success version that retains error information rather than silently returning a plausible complete overview.

Explain before moving on

What happens to the other operation when one rejects? Why is a rejected overview not a rollback?

Was this article helpful?

Your response is saved on this device.