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.
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.
- 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.html
that loads and runs amain
function frommain.js
.
- Create a
main.js
file with amain
function that creates an object namedrocket
:
export function main() { console...