Last active
August 29, 2015 14:12
-
-
Save davidvanvickle/1789ebb2179a9b3b9d60 to your computer and use it in GitHub Desktop.
JS promises notes
This file contains hidden or 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
// http://www.html5rocks.com/en/tutorials/es6/promises/ | |
function get(url) { | |
// Return a new promise. | |
return new Promise(function(resolve, reject) { | |
// Do the usual XHR stuff | |
var req = new XMLHttpRequest(); | |
req.open('GET', url); | |
req.onload = function() { | |
// This is called even on 404 etc | |
// so check the status | |
if (req.status == 200) { | |
// Resolve the promise with the response text | |
resolve(req.response); | |
} | |
else { | |
// Otherwise reject with the status text | |
// which will hopefully be a meaningful error | |
reject(Error(req.statusText)); | |
} | |
}; | |
// Handle network errors | |
req.onerror = function() { | |
reject(Error("Network Error")); | |
}; | |
// Make the request | |
req.send(); | |
}); | |
} | |
function getJSON(url) { | |
return get(url).then(JSON.parse); | |
} | |
var promise = new Promise(function(resolve, reject) { | |
resolve(1); | |
}); | |
promise.then(function(val) { | |
console.log(val); // 1 | |
return val + 2; | |
}).then(function(val) { | |
console.log(val); // 3 | |
}); | |
asyncThing1().then(function() { | |
return asyncThing2(); | |
}).then(function() { | |
return asyncThing3(); | |
}).catch(function(err) { | |
return asyncRecovery1(); | |
}).then(function() { | |
return asyncThing4(); | |
}, function(err) { | |
return asyncRecovery2(); | |
}).catch(function(err) { | |
console.log("Don't worry about it"); | |
}).then(function() { | |
console.log("All done!"); | |
}); | |
getJSON('story.json').then(function(story) { | |
return getJSON(story.chapterUrls[0]); | |
}).then(function(chapter1) { | |
addHtmlToPage(chapter1.html); | |
}).catch(function() { | |
addTextToPage("Failed to show chapter"); | |
}).then(function() { | |
document.querySelector('.spinner').style.display = 'none'; | |
}); | |
// 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); | |
}); | |
}); | |
// Loop through our chapter urls | |
story.chapterUrls.reduce(function(sequence, chapterUrl) { | |
// Add these actions to the end of the sequence | |
return sequence.then(function() { | |
return getJSON(chapterUrl); | |
}).then(function(chapter) { | |
addHtmlToPage(chapter.html); | |
}); | |
}, Promise.resolve()); | |
getJSON('story.json').then(function(story) { | |
addHtmlToPage(story.heading); | |
return story.chapterUrls.reduce(function(sequence, chapterUrl) { | |
// Once the last chapter's promise is done… | |
return sequence.then(function() { | |
// …fetch the next chapter | |
return getJSON(chapterUrl); | |
}).then(function(chapter) { | |
// and add it to the page | |
addHtmlToPage(chapter.html); | |
}); | |
}, Promise.resolve()); | |
}).then(function() { | |
// And we're all done! | |
addTextToPage("All done"); | |
}).catch(function(err) { | |
// Catch any error that happened along the way | |
addTextToPage("Argh, broken: " + err.message); | |
}).then(function() { | |
// Always hide the spinner | |
document.querySelector('.spinner').style.display = 'none'; | |
}); | |
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