Can I break a chain early with bluebird Promises?

后端 未结 3 1873
感情败类
感情败类 2021-01-12 12:08

I don\'t necessarily want to error, but I have:

getFromDb().then (tradeData) ->
  if not tradeData
    # DO NOT CONTINUE THE CHAIN
  else
    getLatestPri         


        
3条回答
  •  猫巷女王i
    2021-01-12 12:54

    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
    });
    

提交回复
热议问题