-
-
Save joepie91/c5f99a18975df0bf2f98 to your computer and use it in GitHub Desktop.
"Breaking out" of a promises chain
This file contains hidden or 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(function(value){ | |
if (value === 3) { | |
return "final value"; | |
} else { | |
return Promise.try(function(){ | |
return someOtherAsyncThing(); | |
}).then(function(secondValue){ | |
return getFinalValue(secondValue); | |
}); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do we need the second
Promise.try
if we're already in the Promise chain? Isn't the result the same?