Modules and Async Programming Fundamentals
Organize code across files with import/export, and handle time-delayed work with Promises.
What you'll learn
- Explain what import/export are for at a conceptual level
- Explain why asynchronous operations exist and how callbacks led to Promises
- Use async/await to write asynchronous code that reads top to bottom
Prerequisites
Explanation
As programs grow, you split code across multiple files instead of one giant script. JavaScript modules let one file share code with another using export (to make something available) and import (to pull it in elsewhere):
// math.js
export function add(a, b) { return a + b; }
// app.js
import { add } from "./math.js";
console.log(add(2, 3));
This keeps related logic together, avoids naming collisions between files, and makes large codebases navigable. (This sandboxed lesson runs everything in one file, so you won't write real import/export here — but you'll use exactly this pattern once you work with real project files.)
Separately, some operations don't finish instantly — fetching data from a server, reading a large file, waiting on a timer. JavaScript handles these asynchronously: instead of freezing the whole program until the slow thing finishes, it keeps running and gets notified later.
The old way to handle this was callbacks (a function passed in to run "when done"), which becomes hard to read once you chain several async steps ("callback hell"). Modern JavaScript uses Promises instead — an object representing a value that will exist eventually, either successfully (resolved) or with an error (rejected):
function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
wait(100).then(() => console.log("100ms passed"));
async/await is syntax sugar over Promises that lets asynchronous code read like ordinary top-to-bottom code:
async function run() {
console.log("start");
await wait(100);
console.log("100ms later");
}
Any function marked async automatically returns a Promise, and await pauses that function (without freezing the rest of the page) until the awaited Promise settles. You'll use this constantly once you start fetching real data in the next lesson.
Example
A Promise-returning helper used with async/await.
function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function run() {
console.log("start");
await wait(50);
console.log("finished waiting");
}
run();Try it yourself
Add a second console.log after another await wait(...) call.
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Guided exercise
Guided exercise
Write an async function `delayedDouble(n)` that waits 10ms (using the provided wait helper) and then returns n * 2.
Checks: delayedDouble(4) resolves to 8
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Stuck? Get a hint.
Independent exercise
Independent exercise
Write an async function `fetchThenAdd(a, b)` that awaits the provided mockFetchNumber() helper (which resolves to 100) and returns a + b + that resolved number.
Checks: fetchThenAdd(1, 2) resolves to 103 · plus 1 hidden check
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Stuck? Get a hint.
Common mistakes
- Forgetting the async keyword on a function that uses await inside it.
- Not awaiting (or .then-ing) a Promise, then being confused why you have a Promise object instead of the actual value.
- Assuming await pauses the entire page — it only pauses the async function it's written in.
Knowledge check
Takeaway
Modules organize code across files; async/await lets time-delayed work read like ordinary sequential code.
Summary
import/export split code across files. Asynchronous operations don't block the rest of the program; Promises represent their eventual result, and async/await lets you write that logic in a readable, top-to-bottom style.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.