20 lines
431 B
Markdown
20 lines
431 B
Markdown
|
|
# Rewrite using async/await
|
|
|
|
Rewrite this example code from the chapter <info:promise-chaining> using `async/await` instead of `.then/catch`:
|
|
|
|
```js run
|
|
function loadJson(url) {
|
|
return fetch(url)
|
|
.then(response => {
|
|
if (response.status == 200) {
|
|
return response.json();
|
|
} else {
|
|
throw new Error(response.status);
|
|
}
|
|
})
|
|
}
|
|
|
|
loadJson('no-such-user.json')
|
|
.catch(alert); // Error: 404
|
|
```
|