1. Declare async
An async function always returns a promise, so callers can await or chain it.
async function load(url) {Typorithm · medium
Step 1 of 8 · intro card
Async
An async function always returns a promise. await pauses until a promise fulfills and unwraps its value, and try/catch turns a rejected promise into a normal catchable error.
Marking a function async makes it return a promise no matter what; a plain return value becomes a fulfilled promise and a thrown error becomes a rejected one.
await may only appear inside an async function. It pauses execution until the awaited promise settles, then evaluates to the fulfilled value — so await fetch(url) gives you the response directly instead of a promise.
Because a rejected awaited promise throws, you wrap awaits in try/catch: any failure from fetch or res.json() lands in the catch block as a normal error.
async functions always return a promise.
await unwraps a promise's fulfilled value.
try/catch handles rejected awaited promises.
async function load(url) {
try {
const res = await fetch(url);
const data = await res.json();
return data;
} catch (e) {
console.error(e);
}
}Connect each part of the code to the concept before you type.
An async function always returns a promise, so callers can await or chain it.
async function load(url) {try wraps the awaited calls so a rejection becomes a catchable error.
try {await pauses the function until the fetch promise fulfills, unwrapping the response.
const res = await fetch(url);await unwraps the second promise from res.json() into the parsed data.
const data = await res.json();A rejected awaited promise throws here, where catch handles it.
} catch (e) {Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.