So far in this chapter, we've seen how to work with async
functions that fulfill successfully. But, as we know, this is not always the case. We need to be able to handle errors that are thrown by asynchronous functions, or any functions they call.
In this recipe, we'll see how try-catch
blocks can handle errors that are thrown by async
functions.
This recipe assumes 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
4-07- async-errors-try-catch
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create an
async
function,addBoosters
, that throws some error:
async function addBoosters() { throw new Error('Unable to add Boosters'); }
- Create an
async
function,performGuidanceDiagnostic
, that also throws an error...