Skip to content

Instantly share code, notes, and snippets.

@joepie91
Created May 18, 2015 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joepie91/c5f99a18975df0bf2f98 to your computer and use it in GitHub Desktop.
Save joepie91/c5f99a18975df0bf2f98 to your computer and use it in GitHub Desktop.
"Breaking out" of a promises chain
/* Short answer: don't. Use conditional branches instead. See below. */
Promise.try(function(){
return someAsyncThing();
}).then(function(value){
if (value === 3) {
return "final value";
} else {
return Promise.try(function(){
return someOtherAsyncThing();
}).then(function(secondValue){
return getFinalValue(secondValue);
});
}
})
@feimosi
Copy link

feimosi commented May 13, 2017

Do we need the second Promise.try if we're already in the Promise chain? Isn't the result the same?

@RyanBreaker
Copy link

Any chance you can edit with whitespace to be more readable? Thanks!

@joepie91
Copy link
Author

@RyanBreaker How do you mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment