How should I catch errors from an async function called by another function?
Quick answer
If an async function awaits another async function, put try/catch around the await when that function can make a useful local decision about the failure. If it cannot resolve the problem, let the rejection propagate so a caller can handle it. Catch optional work locally when you can safely continue; do not turn a failed critical operation into success by returning a misleading fallback. [S1]
The question, the details, the next step.
A practical checklist
Mark each awaited operation as required or optional for this specific flow.
Place a local
try/catchonly where the function can make a sound recovery decision.Let failures that the current function cannot resolve propagate to a caller that can respond.
Use a fallback only when it is an explicitly acceptable result and downstream code supports it.
Walk through one required-operation failure and one optional-operation failure to verify both paths.
A few more useful answers
Does `try`/`catch` work around an `await`?
Yes. If the awaited promise rejects, a surrounding try/catch can handle that failure. The catch handles only failures that reach its scope; a narrower catch may handle a particular step locally. [S1]
Should every async function catch its own errors?
No. Catch inside a function when it can recover or intentionally choose a valid fallback. Otherwise, let the rejection propagate to a caller that can decide what to do. [S1]
Is returning an empty value from `catch` the same as propagating an error?
No. Returning a fallback handles the failure by producing a value, so callers may no longer see a rejection. Use that approach only when the fallback is valid for the application.
Sources
References used for this guide.
- [S1] Using promises — JavaScript | MDN developer.mozilla.org · Checked
