Skip to content
Aabha AI Academy
Skip to reading

JavaScript · Module 9

Answers and explanations · Wait for data with promises and fetch

Compare these references after your own attempt. You can read them at any time; they do not record a live model or vendor run.

A: the first await obtains the response; the second reads and parses its body. A 404 response normally fulfills the fetch promise and requires an explicit response.ok check. JSON syntax alone does not prove the fields are usable.

B: normal shows six events, empty shows a distinct empty message, and the other three show failure with a retryable button. A missing file has HTTP 404; malformed JSON fails parsing; wrong shape fails our own check. finally restores controls for all these paths.

C: replace the existing shape test with:

const valid = Array.isArray(data) && data.every((item) => {
  return item !== null && typeof item === "object"
    && typeof item.title === "string" && item.title.trim() !== ""
    && Number.isInteger(item.places) && item.places >= 0;
});
if (!valid) throw new Error("Invalid event data");

A failing fixture is [{"title":"Garden morning","places":-1}]. The capstone's validator goes further by checking unique IDs, allowed categories and date-only values. Those checks reflect its renderer's contract, rather than a claim that every imaginable data problem is covered.

Return to the exercise

Return to this module