From the course: JavaScript: Async

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Using statements to handle errors in async/await code

Using statements to handle errors in async/await code - JavaScript Tutorial

From the course: JavaScript: Async

Using statements to handle errors in async/await code

- [Instructor] Async in await statements don't come with their own error handling methods like promises do. But the async and await structure lets us write code that looks a lot like synchronous JavaScript. And it turns out that it also works just fine with standard JavaScript error handling statements. You include your statements in a try block and then add a catch block afterward that specifies a parameter name for the error object and includes code to handle the error. So for our Explorer California site, I can update the async function to handle errors. And so first off, I'm going to add a try block and I want to move all of my codes so far inside the function. I want to move that actually inside the try block. So leaving those last two lines down here that close the async function and that close our event listener. And so now all of that code, including the await statements is in the try block. And so after the try…

Contents