Created
August 20, 2018 18:38
-
-
Save djp424/73197dd3245f756d620af2de46513d56 to your computer and use it in GitHub Desktop.
Asynchronous fetch JSON data from URLs with Promises (Fetch)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get data from list of URL's in asynchronous. | |
function asyncRequester(url) { | |
return fetch(url).then((response) => response.json()); | |
} | |
var urls = ['temp.json', 'temp.json', 'temp.json', 'temp.json', 'temp.json'], | |
pall = [], | |
resu = document.querySelector('#results'); | |
for(var i in urls) { | |
pall.push(asyncRequester(urls[i])); | |
} | |
Promise.all(pall).then(function(results) { | |
for(var i in results) { | |
var li = document.createElement('li'), | |
text = document.createTextNode(results[i][0]['title']['rendered']); | |
li.appendChild(text); | |
resu.appendChild(li); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment