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);
}

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(...).

Further reading

Problem with a question

Node.js · Junior · question 67

Test yourself

Take the Node.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz