The await Keyword
The await Keyword — the core idea
await pauses the continuation of an async function until a promise-like value settles. The rest of the application can keep processing events. Sequential awaits are sometimes required, but independent operations can be started together.
What you’ll learn
- Explain the await keyword with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: The first await unwraps "one" before the call for "two" begins. The second await unwraps "two" and the final log shows the ordered flow.
Keep the boundary clear: Avoid a loop of sequential awaits when independent requests could safely run concurrently; start the promises first when appropriate.
See the idea as a flow
Run the complete TypeScript example
Save the code as await-keyword.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
async function step(label: string): Promise<string> { return Promise.resolve(label); }
async function main(): Promise<void> {
const first = await step("one");
const second = await step("two");
console.log(`${first} -> ${second}`);
}
main().catch(console.error);Verified output
one -> twoBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable await-keyword.ts; run: node await-keyword.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
The first await unwraps "one" before the call for "two" begins. The second await unwraps "two" and the final log shows the ordered flow.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Avoid this common mistake
Avoid a loop of sequential awaits when independent requests could safely run concurrently; start the promises first when appropriate.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Call step("A") and step("B") before awaiting either and compare when the work starts.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
await pauses the continuation of an async function until a promise-like value settles.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.