Error Handling with async/await

Learn error handling using try... catch with async/await.

Async/await doesn’t just improve the readability of asynchronous code under standard conditions, but it also helps when handling errors. In fact, one of the biggest gains of async/await is the ability to normalize the behavior of the try... catch block, to make it work seamlessly with both synchronous throwsand asynchronous Promise rejections. Let’s demonstrate that with an example.

A unified try...catch experience

Let’s define a function that returns a Promise that rejects with an error after a given number of milliseconds. This is very similar to the delay() function that we already know very well.

Get hands-on with 1200+ tech skills courses.