In the last lesson we learned how to use async and await to make asynchronous code look synchronous but we didn't handle any errors. In this lesson we will see how to handle errors in async/await code.
To handle errors in async/await code, we use the try...catch block. Let's incorporate error handling to the code we wrote in the previous lesson.
In the above example, we've wrapped the code inside the async function in a try...catch block. If any of the promises inside the async function reject, the error will be caught by the catch block and logged to the console.
Remember the finally method we learned about in the finally lesson? Similarly, in try...catch we have finally block. The finally block will run regardless of whether the promises inside the async function are fulfilled or rejected. Let's add finally block to the code above.
In the above example, the console will log Rocket launch process completed. regardless of whether the rocket launch succeeds or fails.