Skip to content

Instantly share code, notes, and snippets.

@benmccormick
Created December 24, 2015 03:23
Show Gist options
  • Save benmccormick/434d57b05666030c1a8b to your computer and use it in GitHub Desktop.
Save benmccormick/434d57b05666030c1a8b to your computer and use it in GitHub Desktop.
Waiting on multiple async operations using Promises
//fetch makes an HTTP request and returns a promise that resolves
//when the file is loaded
let p1 = fetch('/data/file1.json');
let p2 = fetch('/data/file2.json');
let p3 = fetch('/data/file3.json');
Promise.all([p1,p2,p3]).then(function(values) {
//values contains the responses from each of the promises
let [response1, response2, response3] = values;
doStuffWithResponses(response1, response2, response3);
}).catch(function() {
alert('failed to load one of the files')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment