Created
November 25, 2015 11:01
-
-
Save jpina/782e804bf705111683fd to your computer and use it in GitHub Desktop.
07 Parallelism
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
Promise.all(arrayOfPromises).then(function(arrayOfResults) { | |
//... | |
}); | |
/*****************************************************************/ | |
getJSON('story.json').then(function(story) { | |
addHtmlToPage(story.heading); | |
// Take an array of promises and wait on them all | |
return Promise.all( | |
// Map our array of chapter urls to | |
// an array of chapter json promises | |
story.chapterUrls.map(getJSON) | |
); | |
}).then(function(chapters) { | |
// Now we have the chapters jsons in order! Loop through… | |
chapters.forEach(function(chapter) { | |
// …and add to the page | |
addHtmlToPage(chapter.html); | |
}); | |
addTextToPage("All done"); | |
}).catch(function(err) { | |
// catch any error that happened so far | |
addTextToPage("Argh, broken: " + err.message); | |
}).then(function() { | |
document.querySelector('.spinner').style.display = 'none'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment