Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Asynchronous Programming with JavaScript!

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Since you're working with what looks like synchronous code, async functions can use try...catch
to handle any errors. try...catch
is the most common way to handle exceptions when using async/await.
Resources
- try...catch – MDN
- return await promiseValue vs. return promiseValue
- throw – MDN
- console.error() – MDN
Using try...catch
and async/await in the event listener
btn.addEventListener('click', async (event) => {
event.target.textContent = 'Loading...';
try {
const astros = await getPeopleInSpace(astrosUrl);
generateHTML(astros);
} catch(e) {
peopleList.innerHTML = '<h3>Something went wrong!</h3>';
console.error(e);
} finally {
event.target.remove();
}
});
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Samuel Kleos
Front End Web Development Techdegree Student 13,728 Points0 Answers
-
Alex Weissman
720 PointsWhy do we need to catch and re-throw the error in getJSON?
Posted by Alex WeissmanAlex Weissman
720 Points1 Answer
-
Ben McMahan
7,922 Points6 Answers
-
Justin Kao
5,724 Points3 Answers
-
Justin Kao
5,724 Points2 Answers
-
Tomasz Grodzki
8,130 Points0 Answers
-
wc93
25,725 PointsThe event listener try -catch code snippet does not seem to work if something is wrong with the urls
3 Answers
-
James Croxford
12,723 Points0 Answers
-
PLUS
Steve Seebart
Courses Plus Student 11,897 Points14 Answers
-
Ahmed Omar Ali
Front End Web Development Techdegree Student 11,653 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up