Node.js · Junior · Question 67 of 75
How do you catch an error from an awaited promise?
async function load() {
const data = await readFile("config.json", "utf8");
return JSON.parse(data);
}- ✓Correct answer: Wrap the code in
try { ... } catch (err) { ... } - 2Errors in async functions can't be caught
- 3Add
.error()after the function
Explanation
Inside an async function, await turns a rejection into a thrown error, so a normal try/catch works. Callers can also use load().catch(...).