Tuesday, 20 September 2016

What is the correct terminology for javascript promises




I'm getting confused with the different terminology thrown around. From my understanding, a promise can be:



fulfilled
rejected
pending
settled
resolved
defer



Does resolve mean settled? or does it mean its fulfilled? and what the heck is defer?


Answer



Terminology can be hard.
Let's take from the Promises/A+ specification and the respective ES6 section that there are 3 states:




  • pending - the promise does not have taken a value yet, it's future is still uncertain.

  • fulfilled - the promise successfully got a result value "assigned"

  • rejected - the promise is given a reason why no result could be acquired, typically an error.




The term settled is a hyperonym for fulfilled and rejected, meaning either - the opposite of pending.



The dynamic verbs fulfill and reject describe changing the state from pending into either the fulfilled or rejected. These transitions are called fulfillment or rejection of the promise.



Those were easy. Now, resolve is a different beast. It sometimes is used synonymous to "fulfill", but it would better be understood as settling the promise's fate to either fulfilled or rejected. The resolution (seldom: settlement) of a promise means that it leaves the pending state. But not even that is accurate - the problem is the recursive nature of the Promise Resolution Procedure:




  • resolving a promise with a "plain" value means fulfilling it

  • resolving a promise with a promise (or thenable) means adopting its state:





    • resolving with a fulfilled promise is a fulfillment

    • resolving with a rejected promise is a rejection

    • resolving with a pending promise means waiting for its resolution




Yes, if a promise is resolved it might not even be known whether it's going to be fulfilled or rejected. But it means the fate is no longer undetermined, as it's bound to the promise that we resolved with (notice that you can resolve a promise only once).



Ignoring this special case, a resolved promise usually means a settled promise.



Or, to cite the ECMAScript 6 Specification:




A promise is resolved if it is settled or if it has been “locked in” to match the state of another promise. Attempting to resolve or reject a resolved promise has no effect. A promise is unresolved if it is not resolved. An unresolved promise is always in the pending state. A resolved promise may be pending, fulfilled or rejected.









and what the heck is defer?




Deferring a result means that you return an (asynchronous) promise for the result, and not the result directly (synchronously). And also return a deferred rejection instead of throwing synchronously.



Notice that "defer" is also used in some libraries (Q) as the method name to construct a Deferred object - see this answer on The differences between Deferred, Promise and Future for a good explanation.
Oh, and never trust variable names: defer might as well be an abbreviated "deferredObject".


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...