Skip to content

Instantly share code, notes, and snippets.

@RyanBreaker
Forked from joepie91/.js
Last active July 24, 2019 00:44
Show Gist options
  • Save RyanBreaker/a962b0dec1b17d0e439aade60747deaa to your computer and use it in GitHub Desktop.
Save RyanBreaker/a962b0dec1b17d0e439aade60747deaa 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( (value) => {
if (value === 3) {
return "final value";
} else {
return Promise.try( () => {
return someOtherAsyncThing();
}).then( (secondValue) => {
return getFinalValue(secondValue);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment