-
-
Save RyanBreaker/a962b0dec1b17d0e439aade60747deaa to your computer and use it in GitHub Desktop.
"Breaking out" of a promises chain
This file contains 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
/* 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