Rewrite using async/await
Rewrite this example code from the chapter Promises chaining using async/await instead of .then/catch:
The notes are below the code:
Notes:
-
The function
loadJsonbecomesasync. -
All
.theninside are replaced withawait. -
We can
return response.json()instead of awaiting for it, like this:if (response.status == 200) { return response.json(); // (3) }Then the outer code would have to
awaitfor that promise to resolve. In our case it doesn’t matter. -
The error thrown from
loadJsonis handled by.catch. We can’t useawait loadJson(...)there, because we’re not in anasyncfunction.