When to reject/resolve a promise

后端 未结 2 2064
独厮守ぢ
独厮守ぢ 2021-01-07 20:15

I am thinking about when exactly I need to reject a promise. I found a couple of questions regarding this topic, but could not find a proper answer. When should I reject a p

2条回答
  •  遥遥无期
    2021-01-07 20:51

    I know where you're coming from. Q and Q documentation can quite easily have you believe that deferred/promise rejection is all about exception handling.

    This is not necessarily the case.

    A deferred can be rejected for whatever reason your application requires.

    Deferreds/promises are all about handling responses from asynchronous processes, and every asynchronous processes can result in a variety of outcomes - some of which are "successful" and some "unsuccessful". You may choose to reject your deferred - for whatever reason, regardless of whether the outcome was nominally successful or unsuccessful, and without an exception ever having been thrown, either in javascript or in the asynchronous process.

    You may also choose to implement a timeout on an asynchronous process, in which case you might choose to reject a deferred without a response (successful or unsuccessful) having been received. In fact, for timeouts, this is what you would typically choose to do.

提交回复
热议问题