问题
I don't necessarily want to error, but I have:
getFromDb().then (tradeData) ->
if not tradeData
# DO NOT CONTINUE THE CHAIN
else
getLatestPrice tradeData
.then (latestPrice) ->
...
.then ->
...
.then ->
...
.catch (err) ->
next err
Any way for me to abort the chain if there is no tradeData?
回答1:
getFromDb().then (tradeData) ->
if tradeData
getLatestPrice tradeData ->
.then (latestPrice) ->
...
.then ->
...
.then ->
...
.catch (err) ->
next err
else
getSomethingElse ->
send("no data")
In 3.0, you will be able to do this:
p = getFromDb().then (tradeData) ->
if not tradeData
send("no data");
p.break()
else
getLatestPrice tradeData
.then (latestPrice) ->
...
.then ->
...
.then ->
...
.catch (err) ->
next err
回答2:
Although an accepted answer, but I would like to tell all of googlers that, "break()" function has been changed to "cancel()"
Use Something like this:
p = getFromDb().then (tradeData) ->
if not tradeData
send("no data");
p.cancel(); // Look Here!!!!!!!!!!!!!!!!
else
getLatestPrice tradeData
.then (latestPrice) ->
...
.then ->
...
.then ->
...
.catch (err) ->
next err
Before that, make sure to add following lines in config:
Promise.config({
cancellation: true
});
回答3:
Im just wondering why not taking a benefit of fact that you can throw
whatever you like, not just something that is instanceof Error
. It is considered as bad practice to do that? In my opinion it depends, it depends on what you are trying to accomplish. Promise chain can be interrupted by various reasons, but in general those two would fall into two groups. Classic error occur
and early break in chain
needed. Logically second one can't be considered as something that should be an instance of Error
.
const handleError = (err) => {
...
}
const skip = (reason, ..., ...) => {
/**
* construct whatever you like
* just for example here return reason
*/
return reason
}
Promise.resolve()
.then(() => {
if (iShouldEndChainEarlier) {
throw skip('I would like to end chain earlier')
}
return asyncOperation1()
})
.then(results => {
...
return asyncOperation2(results)
})
.then(... => {
...
})
.catch(interrupt => {
if (interrupt instanceof Error) {
return handleError(interrupt)
}
/**
* Handle breaking promise chain earlier
* having interrupt reason in scope
*/
})
If logically, early break in chain can be considered as error(which can totally be the case), you could create your custom error and differentiate between the two in catch
block. So just saying that one could think about another approach(s) when handling whatever interrupt that can occur in promise chain.
We could argue does this can be considered as something against first error pattern
in node. If there is an error best practice would be to invoke callback like callback(err)
where err
really should be instanceof Error
otherwise callback(null, data)
. But on other side having in mind that .catch(fn)
is just sugar for then(undefined, onRejected)
to me it seems good enough to handle onRejected
parameter based on situation you are in.
来源:https://stackoverflow.com/questions/28572180/can-i-break-a-chain-early-with-bluebird-promises