Skip to content

Instantly share code, notes, and snippets.

@tohagan
Created August 25, 2018 02:22
Show Gist options
  • Save tohagan/d45f57a8b60bbe859f375c567d80b52e to your computer and use it in GitHub Desktop.
Save tohagan/d45f57a8b60bbe859f375c567d80b52e 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);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment