“Intentionally” means something other than “fulfillment”?

(Related, but not quite the same: JS Promises: Fulfill vs Resolve )

I am trying to wrap my head around Javascript promises, and I am struggling with the basic concepts of permission and permission, against execution and execution. I read several submissions such as Jake Archibald , as well as browsing through some relevant specifications .

In States and Fates (not a completely official specification, but referred to as an authoritative document written by one of the authors of the performances), fulfilling this state when allowed is a "fate" (no matter what it is, but they are distinctly different):

Promises have three possible mutually exclusive conditions: completed, rejected, and expected.

  • The promise is fulfilled if promise.then(f) calls f "as soon as possible."

and

A promise is allowed when trying to allow or reject , it has no effect, that is, the promise was “blocked” to either follow another promise or fulfilled or rejected

In particular, permitted covers both completed and rejected (and blocked). The "opposite" (or the directly corresponding function) for rejection is performed, not allowed; A solution includes rejection as one of its capabilities.

However, spec refers to the first argument to the then() method (or its corresponding abstract concepts), using both execution and resolution:

  • 25.4: The promise p is fulfilled if p.then ( f , r) immediately places the task to call the function f.

  • 25.4.1.1: [[ Allow ]] Function Object The function that is used to resolve this promise object.

  • 25.4.1.3. Set the internal slot [[Promise]] resolution . Set already [[AlreadyResolved]] internal slot to allow to already allowed. [Then immediately, failure is used in exactly the same way.]

  • 25.4.5.3: Promise.prototype.then ( onFulfilled , onRejected)

Perhaps one of the most important is

  • 25.4.4.5 Promise. allow (x)

which MDN describes as follows:

The Promise.resolve (value) method returns a Promise object that is allowed with the given value. If the value is obscured (that is, Has a "then" method), the returned promise "will follow" this, assuming its possible state; otherwise, the returned promise will be fulfilled with a value.

There is no mention of the Promise.resolve() method, which may be rejected. In addition, there is a Promise.reject() method, but not a Promise.fulfill() method. So here, the opposite, to reject, is a decision, not an implementation.

Of course, there can be no guaranteed correlation between the permitted term "fate" and the decision of a method (or verb). But it would be (is it?) Really misleading and confusing so that resolve() put the promise in a specially executed state when the terms fulfilled and resolved were carefully defined in order to have different meanings.

This is what happened here ... the right hand did not know what the left hand was doing, and we ended up with the documents, which should be the guiding lights of the movement, using the terms in inconsistent ways? Or is there something that I am missing, and resolving is actually a more suitable term than fulfilling the resolve() method?

I am not going to criticize the authors of the document. I understand how difficult it is to get all the conditions that will be used consistently in all documents with a widely distributed history group. My goal here, delving into the terminology and wording of these documents, is to clearly understand the terms, which include knowing the limits of how accurately terms can be defined, such as implementation and resolution. Jake Archibald admits that he sometimes mixes the terms. This is a very useful confession for noobs like me who are trying to understand terminology! Thank you, Jake, for being vulnerable. :-) My goal in asking this question is to find out which definitions or applications of these terms are reliable? Or should I conclude that a solution is sometimes used specifically to mean fulfillment, and sometimes execute / reject / block even in the most authoritative documents?

+6
source share
2 answers

I struggle with the basic concepts of permission and permission, against fulfillment and fulfillment.

See What is the correct terminology for javascript promises . Yes, they are sometimes confused, but try to ignore it.

However, the specification refers to the first argument of the then() method (or its corresponding abstract concepts), using both execution and resolution. Have we received documents that should be indicative traffic lights using terms in inconsistent ways?

No, there is no inconsistency, the terms in the specification are accurate.

Decision

is not the opposite of rejection, and the onFulfilled does not exactly match the resolution.

Or is there something that I am missing, and resolving is actually a more suitable term than fulfilling the resolve() method?

Yes. The problem is that you can solve with the promise (or, in general, thenable, i.e., any object with the then method), and instead of fulfilling this object, the resolved promises will try to fulfill with the subsequent result. Or when thenable has (will) an error, the reason is rejected.

There is no mention [in MDN] of the Promise.resolve() method, which has the possibility of rejection.

Actually there is: "the returned promise will" follow ", which then, accepting its possible state." If this final state is an error state, then the returned promise will also be rejected.

There is a Promise.reject() method, but not a Promise.fulfill() method. So here, the opposite, to reject, is a decision, not an implementation.

Yes, ES6 promises is missing the Promise.fulfill method. At first it was a little inconsistent and confusing, but it was done for a good reason: it prevents you from fulfilling a promise that is fulfilled with another promise. You can only allow ES6 promises, and when you pass the promise, it will not take the result of the promise itself by itself.

This is very useful, but can be quite inconvenient. There are libraries that do this better and solve this case, they are the so-called "algebraic promises" (which you can talk much better about). Creed is a good example of this, and its Promise.of method is the real equivalent of Promise.reject .

+2
source

Add an accent elsewhere in one of your quotes:

A promise is permitted if an attempt to allow or reject it has no effect, that is, the promise was “blocked” to either follow another promise , or was fulfilled or rejected

reject() will change your promise and all previous statuses to rejected .
For now, resolve() block your current promise, but only the execution of allowing callbacks will set its status to fulfilled . Until everything is finished, your promise will still be 'pending' .

For example, if you make a chain of your promise and that an error occurs during the chain, then the status of your promise is set to 'rejected' .

 var p = new Promise((resolve, reject) => { resolve(); // here p is resolved, we can't call resolve() or reject() anymore }).then((e) => { // chain it return new Promise((resolve, reject) => { console.log(p); // 'pending' because it still chained reject('whatever') // this one throws an error and breaks the chain }) }).then(() => console.log('passed')); // won't happen setTimeout(() => console.log(p, "please check in your browser console"), 1000); // rejected 

So, even if you resolve() current operation of your promise, you cannot know what will happen after that, so you cannot know whether it will be fulfilled or rejected at the end.

+1
source

Source: https://habr.com/ru/post/1014516/


All Articles