fetch/ asyn

Promise

  1. function getFromGitHub() {
  2.   const userName = 'dupontdenis';
  3.   const url = 'https://api.github.com/users';

  4.   fetch(`${url}/${userName}/repos`)
  5.     .then(reposResponse => {
  6.       return reposResponse.json();
  7.     })
  8.     .then(userRepos => {
  9.       console.log(userRepos[0].owner.avatar_url)
  10.       document.body.insertAdjacentHTML('afterbegin',`<img src=${userRepos[0].owner.avatar_url}>`)
  11.     })
  12.     .catch(err => {
  13.       console.log(err);
  14.     });
  15. }

  16. getFromGitHub();

Avec sync


  1. async function getFromGitHub() {
  2.   try {
  3.     const userName = 'dupontdenis';
  4.     const url = 'https://api.github.com/users';
  5.     const reposResponse = await fetch(`${url}/${userName}/repos`);
  6.     const userRepos = await reposResponse.json();

  7.           document.body.insertAdjacentHTML('afterbegin',`<img src=${userRepos[0].owner.avatar_url}>`)

  8.   } catch (error) {
  9.     console.log(error);
  10.   }
  11. }


  12. getFromGitHub();