fetch/ asyn
Promise
- function getFromGitHub() {
- const userName = 'dupontdenis';
- const url = 'https://api.github.com/users';
- fetch(`${url}/${userName}/repos`)
- .then(reposResponse => {
- return reposResponse.json();
- })
- .then(userRepos => {
- console.log(userRepos[0].owner.avatar_url)
- document.body.insertAdjacentHTML('afterbegin',`<img src=${userRepos[0].owner.avatar_url}>`)
- })
- .catch(err => {
- console.log(err);
- });
- }
- getFromGitHub();
Avec sync
- async function getFromGitHub() {
- try {
- const userName = 'dupontdenis';
- const url = 'https://api.github.com/users';
- const reposResponse = await fetch(`${url}/${userName}/repos`);
- const userRepos = await reposResponse.json();
- document.body.insertAdjacentHTML('afterbegin',`<img src=${userRepos[0].owner.avatar_url}>`)
- } catch (error) {
- console.log(error);
- }
- }
- getFromGitHub();