Can I break a chain early with bluebird Promises?

后端 未结 3 1868
感情败类
感情败类 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条回答
  •  难免孤独
    2021-01-12 12:56

    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
    

提交回复
热议问题