Skip to content

Instantly share code, notes, and snippets.

@djp424
Created August 20, 2018 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djp424/73197dd3245f756d620af2de46513d56 to your computer and use it in GitHub Desktop.
Save djp424/73197dd3245f756d620af2de46513d56 to your computer and use it in GitHub Desktop.
Asynchronous fetch JSON data from URLs with Promises (Fetch)
// 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