Lesson 21 / 28
try/catch in Async Code
Catching errors from await, and from Promise chains.
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.