Await a rejected promise inside try and catch the error.
Acceptance criteria
- 1.#out should show caught: no network.
JavaScript Tutorial · JavaScript Asynchronous
These 10 tasks go with the JavaScript async Error Handling chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “try and catch handle errors in async functions the same way as anywhere else.” .
1.Catch a rejected awaitexercise · easy · 10 min
Await a rejected promise inside try and catch the error.
Passes when: #out should show caught: no network.
2.Cover several awaitsexercise · easy · 10 min · practice plan
Wrap two awaits in one try and catch the first failure.
Passes when: #out should show step two failed.
3.Clean up with finallyexercise · easy · 10 min · practice plan
Catch the error then report that cleanup ran.
Passes when: #out should show handled and tidied.
4.Catch at the call siteexercise · easy · 10 min · practice plan
Let the error escape and catch it where the function is called.
Passes when: #out should show caught outside: inside.
5.Carry on after catchingexercise · medium · 10 min · practice plan
Catch the failure and report a fallback value.
Passes when: #out should show used a fallback.
6.Fix the try that does not awaitbug fix · easy · 8 min
Without await the rejection escapes the try block.
Passes when: #out should show caught: no network.
7.Fix the catch outside the async functionbug fix · easy · 8 min · practice plan
A plain try around the call cannot catch an async rejection.
Passes when: #out should show caught outside: inside.
8.Fix the second step still runningbug fix · easy · 8 min · practice plan
After a failure the following awaits should be skipped.
Passes when: #out should show step two failed.
9.Fix the error swallowed silentlybug fix · easy · 8 min · practice plan
Catching without reporting hides the failure entirely.
Passes when: #out should show failed: gone.
10.Fix finally used to handle the errorbug fix · easy · 8 min · practice plan
finally always runs but does not catch anything.
Passes when: #out should show handled and tidied.
Between them these tasks cover try and catch, Rejected awaits and finally.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.