How do I break a promise chain?

前端 未结 4 1493
北恋
北恋 2020-12-03 12:19

How should I stop the promise chain in this case? Execute the code of second then only when the condition in the first then is true.

var p = new Promise((res         


        
4条回答
  •  无人及你
    2020-12-03 13:02

    You could read the documentation, which says

    Promise.then return a rejected Promise if the input function throws an error, or the input function returns a rejected Promise.

    If you prefer, you could read the Promise A spec, in the section about then, where promise2 refers to the resulting promise:

    If either onFulfilled or onRejected throws an exception e, promise2 must be rejected with e as the reason.)

    If you prefer, you could read the excellent 2ality blog:

    then() returns a new promise Q (created via the constructor of the receiver): If either of the reactions returns a value, Q is resolved with it. If either of the reactions throws an exception, Q is rejected with it.

    You could read the brilliant YDKJS:

    A thrown exception inside either the fulfillment or rejection handler of a then(..) call causes the next (chained) promise to be immediately rejected with that exception.

提交回复
热议问题