en.javascript.info/1-js/11-async/08-async-await/01-rewrite-async/task.md
2022-02-06 13:05:02 +03:00

456 B

Rewrite using async/await

Rewrite this example code from the chapter info:promise-chaining using async/await instead of .then/catch:

function loadJson(url) {
  return fetch(url)
    .then(response => {
      if (response.status == 200) {
        return response.json();
      } else {
        throw new Error(response.status);
      }
    });
}

loadJson('https://javascript.info/no-such-user.json')
  .catch(alert); // Error: 404