November 2018
Beginner
502 pages
10h 22m
English
In this section, we are going to explore how we can handle errors when using callback code:
try { setTimeout(() => { throw new Error("Something went wrong"); }, 1000);} catch (ex) { console.log("An error has occurred", ex); }
We are again using setTimeout to experiment with callbacks. This time, we throw an error inside the callback. We are hoping to catch the error outside the callback using a try / catch around the setTimeout function.
If we run the code, we see that we don't catch the error:

Read now
Unlock full access