Getting some urls for content, then fetching that content & adding it to the page sequentially. Promises vs node style callbacks vs events.

December 6, 2013 ยท View on GitHub

// With Promises getJson('story.json').then(function(story) { addHtmlToPage(story.heading);

// Map our array of chapter urls // to an array of chapter json promises return story.chapterUrls.map(getJson).reduce(function(chain, chapterPromise) { // Use reduce to chain the promises together, // but adding content to the page for each chapter return chain.then(function() { return chapterPromise; }).then(function(chapter) { addHtmlToPage(chapter.html); }); }, Promise.resolve()); }).then(function() { addTextToPage("All done"); }).catch(function(err) { // catch any error that happened along the way addTextToPage("Argh, broken: " + err.message); }).then(function() { document.querySelector('.spinner').style.display = 'none'; });