Accueil / javascript / 1-js / 11-async / 08-async-await / 01-rewrite-async / task.md

task.md

Réécriture avec async/await

Réécrire cet exemple de code du chapitre <info:promise-chaining> en utilisant async/await au lieu de .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