Skip to content

Instantly share code, notes, and snippets.

@mlms13
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlms13/953aefdc5e5ca0deee2f to your computer and use it in GitHub Desktop.
Save mlms13/953aefdc5e5ca0deee2f to your computer and use it in GitHub Desktop.
Promises and Events! and Pseudo-Code!
function tellMeWhenThatThingHappens() {
var promise = new Promise(function (resolve, reject) {
window.addEventListener('thatAwesomeThingHappens', function () {
resolve(/* maybe with some data here */);
});
// if it's possible for that awesome thing to fail...
window.addEventListener('refusingToBeAwesome', function () {
reject(/* maybe with an error */);
});
});
// do something that might trigger that event
window.maybeMakeAwesomeThingsHappen();
return promise;
}
tellMeWhenThatThingHappens().then(function (data) {
// do something!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment