admin管理员组文章数量:1245096
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?
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?
Share Improve this question asked Feb 17, 2015 at 22:17 ShamoonShamoon 43.6k101 gold badges328 silver badges628 bronze badges 2-
Can you not return a new promise explicitly (one that is resolve-failed as appropriate) from
then
in bb? If just "failing the chain" doesn't work then .. it's not a chain at all. – user2864740 Commented Feb 17, 2015 at 22:19 - You'll want to have a look at Break promise chain and call a function based on the step in the chain where it is broken and Handling multiple catches in promise chain – Bergi Commented Feb 17, 2015 at 23:55
3 Answers
Reset to default 8Although 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
});
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
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 acplish. 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.
本文标签: javascriptCan I break a chain early with bluebird PromisesStack Overflow
版权声明:本文标题:javascript - Can I break a chain early with bluebird Promises? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740140405a2230783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论