Error Handling with async/await
When using async/await, errors from rejected promises are thrown as exceptions inside the async function. This means you can handle them with standard try/catch/finally blocks — the same syntax used for synchronous errors — making error handling consistent across your codebase.
try/catch
Wrap your awaited calls in a try block. If any awaited promise rejects, execution jumps to the catch block with the rejection reason as the caught error.
finally
The finally block runs regardless of success or failure, making it ideal for cleanup tasks like hiding a loading spinner or closing database connections.
Error Propagation
If you do not catch an error in an async function, the returned promise is rejected with that error, propagating it to the caller. This allows you to handle errors at the appropriate level of abstraction.