Handling errors with Promise.catch
In a previous recipe, we saw how to fulfill a promise with an error state using reject, and we saw that this triggers the next catch callback in the promise chain. Because promises are relatively easy to compose, we need to be able to handle errors that are reported in different ways. Luckily promises are able to handle this seamlessly.
In this recipe, we'll see how Promises.catch can handle errors that are reported by being thrown or through rejection.
Getting ready
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
3-07-handle-errors-promise-catch. - Copy or create an
index.htmlthat loads and runs amainfunction frommain.js.
- Create a
main.jsfile with amainfunction that creates an object namedrocket:
export function main() {
console...