From 0615482558a7b82fc57542333d490a16f1976b18 Mon Sep 17 00:00:00 2001 From: DkSanjed Date: Thu, 4 Jun 2020 22:12:31 -0500 Subject: [PATCH] Fetch exercise by raul valencia --- 3-fetch-api-ajax/fetch.html | 11 +++++++++++ 3-fetch-api-ajax/fetch.js | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 3-fetch-api-ajax/fetch.html create mode 100644 3-fetch-api-ajax/fetch.js diff --git a/3-fetch-api-ajax/fetch.html b/3-fetch-api-ajax/fetch.html new file mode 100644 index 0000000..1a66956 --- /dev/null +++ b/3-fetch-api-ajax/fetch.html @@ -0,0 +1,11 @@ + + + + + + Fetch + + + + + \ No newline at end of file diff --git a/3-fetch-api-ajax/fetch.js b/3-fetch-api-ajax/fetch.js new file mode 100644 index 0000000..1374c54 --- /dev/null +++ b/3-fetch-api-ajax/fetch.js @@ -0,0 +1,24 @@ +function loadJson(url) { + return fetch(url) + .then(response => response.json()); +} + +function showAvatar(githubUser) { + return new Promise(function(resolve, reject) { + let img = document.createElement('img'); + let tittle = document.createElement('h1'); + img.src = githubUser.avatar_url; + img.className = "promise-image"; + tittle.innerHTML = `Name: ${githubUser.login}`; + document.body.append(tittle); + document.body.append(img); + + setTimeout(() => { + resolve(githubUser); + }, 3000); + }); +} + +loadJson('https://api.github.com/repos/push-dev/frontend-roadmap/commits') +.then(githubUser => githubUser[0].author) +.then(showAvatar) \ No newline at end of file