Catch a rejected promise and report the message.
Acceptance criteria
- 1.#out should show caught: no network.
JavaScript Tutorial · JavaScript Asynchronous
These 10 tasks go with the JavaScript Promise Errors 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: “catch handles a rejected promise, and finally runs whatever happened.” .
1.Catch a rejectionexercise · easy · 10 min
Catch a rejected promise and report the message.
Passes when: #out should show caught: no network.
2.Catch a thrown errorexercise · easy · 10 min · practice plan
Throw inside a then and catch it.
Passes when: #out should show something broke.
3.Clean up with finallyexercise · easy · 10 min · practice plan
Handle a rejection then report that cleanup ran.
Passes when: #out should show handled and cleaned.
4.Check the error typeexercise · easy · 10 min · practice plan
Report whether the rejection reason is an Error object.
Passes when: #out should show true.
5.Recover from a failureexercise · medium · 10 min · practice plan
Catch a rejection and continue the chain with a fallback value.
Passes when: #out should show used the fallback.
6.Fix the missing catchbug fix · easy · 8 min
The rejection is never handled, so nothing is reported.
Passes when: #out should show handled: gone.
7.Fix the message read from a stringbug fix · easy · 8 min · practice plan
A rejection reason that is a plain string has no message property.
Passes when: #out should show caught: no network.
8.Fix finally expected to receive a valuebug fix · easy · 8 min · practice plan
finally gets no argument; the value passes straight through.
Passes when: #out should show value is here.
9.Fix the throw outside the chainbug fix · easy · 8 min · practice plan
A throw outside a promise handler is not caught by catch.
Passes when: #out should show caught: broke.
10.Fix the chain that stops at the catchbug fix · easy · 8 min · practice plan
The catch should return a value so the next step can use it.
Passes when: #out should show used the fallback.
Between them these tasks cover catch, finally and Error objects.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.