Skip to content

Instantly share code, notes, and snippets.

@coaxial
Forked from joepie91/.js
Last active September 29, 2015 13:18
Show Gist options
  • Save coaxial/a2807b44e953d19368fb to your computer and use it in GitHub Desktop.
Save coaxial/a2807b44e953d19368fb to your computer and use it in GitHub Desktop.
Promise flattening
Promise.try(function(){
return outerThingOne();
}).then(function(value){
return Promise.try(function(){
return innerThingOne();
}).then(function(subValue){
return innerThingTwo(subValue);
});
}).then(function(result){
return outerThingThree(result);
});
/* ... is equivalent to ... */
Promise.try(function(){
return outerThingOne();
}).then(function(value){
return innerThingOne();
}).then(function(subValue){
return innerThingTwo(subValue);
}).then(function(result){
return outerThingThree(result);
});
// How does it differ from:
outerThingOne()
.then(function() { return innerThingOne(); })
.then(function() { return innerThingTwo(); })
.then(function() { return outerThingThree(); })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment