Created
August 25, 2018 02:23
-
-
Save tohagan/63e04d8216a2ca4c2254e92d68c134e4 to your computer and use it in GitHub Desktop.
RoughAverageDictionaries created by tohagan - https://repl.it/@tohagan/RoughAverageDictionaries
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
// 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
Example use of Promise.all() to fetch URLs.