Use modules, closures and async functions
Organize behavior into modules
A module is a unit of JavaScript code with its own scope and an explicit interface. Export values that another module needs, and import them where they are used. This lets a React component live separately from its data helpers. The following two files form one example, not two independent snippets. Save them with the shown names and run node main.mjs from their directory; .mjs selects ECMAScript module syntax in Node.
// labels.mjsexport function formatLabel(name) { return `Task: ${name}`;}export default "Today";// main.mjsimport heading, {formatLabel} from "./labels.mjs";console.log(heading);console.log(formatLabel("Read"));The output is Today followed by Task: Read. The named import uses braces and matches the exported name, unless renamed explicitly with as. A default export is imported without braces and can have a different local name. The backtick string is a template literal: ${name} inserts a value into a string. JSX uses a different syntax and describes elements, not ordinary template strings. Vite module resolution and JSX will be introduced next.
Functions can use surrounding bindings
A closure is a function together with access to the lexical environment in which it was created. A function can be passed to another function or called later while still accessing those bindings. This is useful for callbacks that remember a specific piece of data. It does not imply that a mutable variable is frozen at creation time.
function makeFormatter(prefix) { return name => `${prefix}: ${name}`;}const taskLabel = makeFormatter("Task");const noteLabel = makeFormatter("Note");console.log(taskLabel("Read"));console.log(noteLabel("Call"));Each call to makeFormatter has its own prefix binding. The returned arrow functions access different prefixes, so the output is Task: Read and Note: Call. In React, handlers can similarly access props and state from the render that created them. Render snapshots and stale closures are covered in later chapters.
Wait for a promise with async and await
A promise represents an eventual outcome: pending, fulfilled with a value, or rejected with a reason. An async function always returns a promise. await pauses the rest of that async function until the awaited value is available; it does not block the whole browser. Use try and catch to handle a rejection. The example uses a fixed local value, not a network request.
async function readStatus(available) { if (!available) throw new Error("Unavailable"); return "Ready";}async function report(available) { try { const status = await readStatus(available); console.log(status); } catch (error) { console.log(error.message); }}async function run() { await report(true); await report(false);}run();The output is Ready followed by Unavailable. Throwing inside an async function rejects its promise. The outer run function waits for the first report before starting the second, so the order is predictable. Calling readStatus(true) without awaiting or otherwise consuming its promise does not produce the string directly. In React, async UI also needs explicit loading/error handling and a suitable place to start work; do not start arbitrary side effects during render. Later chapters cover these concerns.
Practice and next chapter
Try a module exporting a formatter, create a callback that reuses it, and handle a rejected promise using synthetic data. These are ordinary JavaScript skills; React builds on them rather than replacing them. Chapter 3 introduces the application entry point and build workflow.