Skip to content

Instantly share code, notes, and snippets.

@tohagan
Created August 25, 2018 02:23
Show Gist options
  • Save tohagan/63e04d8216a2ca4c2254e92d68c134e4 to your computer and use it in GitHub Desktop.
Save tohagan/63e04d8216a2ca4c2254e92d68c134e4 to your computer and use it in GitHub Desktop.
RoughAverageDictionaries created by tohagan - https://repl.it/@tohagan/RoughAverageDictionaries
// This line is only needed for server-side NodeJS.
// You should have fetch() available in your client-side browser.
const fetch = require('node-fetch');
// https://jsonplaceholder.typicode.com - Provides test JSON data
var urls = [
'https://jsonplaceholder.typicode.com/todos/1',
'https://jsonplaceholder.typicode.com/todos/2',
'https://jsonplaceholder.typicode.com/posts/1',
'https://jsonplaceholder.typicode.com/posts/2',
];
// Maps each URL into a fetch() Promise
var requests = urls.map(function(url){
return fetch(url)
.then(function(response) {
// throw "uh oh!"; - test a failure
return response.json();
})
});
// Resolve all the promises
Promise.all(requests)
.then((results) => {
console.log(JSON.stringify(results, null, 2));
}).catch(function(err) {
console.log("returns just the 1st failure ...")
console.log(err);
})
@tohagan
Copy link
Author

tohagan commented Aug 25, 2018

Example use of Promise.all() to fetch URLs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment