Lesson 22 / 30
try/catch in Async Code
Catch errors from await and Promise chains with try/catch, and know what try/catch cannot catch.
Wrapping await
A rejected Promise inside an async function is thrown at the await — catch it with try/catch.
import { readFile } from "node:fs/promises";
async function loadConfig() {
try {
const raw = await readFile("config.json", "utf8");
return JSON.parse(raw);
} catch (err) {
console.error("Could not load config:", err.message);
return {};
}
}try/catch can't wrap callbacks
try/catch only catches synchronous throws (and awaited rejections) — an error thrown inside a plain callback later on the event loop escapes it entirely.