I was reading a document about javascript promises (https://developers.google.com/web/fundamentals/getting-started/primers/promises) and one of the examples uses a sequence of promises.
// Start off with a promise that always resolves
var sequence = Promise.resolve();
// Loop through our chapter urls
story.chapterUrls.forEach(function(chapterUrl) {
// Add these actions to the end of the sequence
sequence = sequence.then(function() {
return getJSON(chapterUrl);
}).then(function(chapter) {
addHtmlToPage(chapter.html);
});
})
I was curious how it worked, since I assumed it would start executing the code when the first .then was added to the promise sequence. When I debugged the code, the promise sequence wasn't executed until the last line of code had been executed within the script tags. So my question is when do promises actually get executed? Thanks.