From 227b31535c98e7afd9c7453ad79efcdcd673228c Mon Sep 17 00:00:00 2001 From: "estefayiso@gmail.com" Date: Thu, 4 Jun 2020 22:12:43 -0500 Subject: [PATCH] Fetch JavaScript exercise --- 3-fetch-api-ajax/fetch.html | 12 ++++++++++++ 3-fetch-api-ajax/fetch.js | 27 +++++++++++++++++++++++++++ 2 files changed, 39 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..77627c0 --- /dev/null +++ b/3-fetch-api-ajax/fetch.html @@ -0,0 +1,12 @@ + + + + + + Document + +
THIS IS AN API'S INFORMATION
+ + + + \ 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..4915d74 --- /dev/null +++ b/3-fetch-api-ajax/fetch.js @@ -0,0 +1,27 @@ + +'use strict' + +const URLS = 'https://api.github.com/repos/push-dev/frontend-roadmap/commits'; + +function loadJson(url) { + return fetch(url) + .then(response => response.json()); + } + + async function showAvatar(githubUser) { + return new Promise(function(resolve, reject) { + let img = document.createElement('img'); + let paragraph = document.createElement('h1'); + img.src = githubUser.avatar_url; + img.className = "promise-image"; + paragraph.innerHTML = 'Name: ' + githubUser.login; + document.body.append(paragraph); + document.body.append(img); + + setTimeout(() => { + resolve(githubUser); + }, 5000); + }); + } + + loadJson(URLS).then(githubUser => githubUser[0].author).then(showAvatar); \ No newline at end of file