-2

I'm confused about the "state" resolved, I don't understand what these texts are referring to.

https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md

Fates

Promises have two possible mutually exclusive fates: resolved, and unresolved.

  • A promise is resolved if trying to resolve or reject it has no effect, i.e. the promise has been "locked in" to either follow another promise, or has been fulfilled or rejected.
  • A promise is unresolved if it is not resolved, i.e. if trying to resolve or reject it will have an impact on the promise.

A promise can be "resolved to" either a promise or thenable, in which case it will store the promise or thenable for later unwrapping; or it can be resolved to a non-promise value, in which case it is fulfilled with that value.

What does the bold text (What does it mean by no effect?) refer to? I am confused

  • 1
    The meaning seems clear (it means literally what it says - bit redundant if you ask me but that’s just my opinion) - what do you not understand exactly? Which words/parts of the phrase? – Clive Mar 11 '23 at 17:53
  • What does it mean by no effect? @Clive –  Mar 11 '23 at 17:55
  • To me it just reads like an awkwardly phrased version of: _A promise is resolved if you can longer resolve it_. Like I said, bit of a pointless addition as it seems obvious. But you’d have to ask the person who wrote it to be sure – Clive Mar 11 '23 at 17:56
  • No effect means nothing happens. – user3840170 Mar 11 '23 at 18:05

2 Answers2

1

"Trying to resolve or reject" refers to calling the resolve and reject functions. Depending on the state of the promise they either resolve and respectively reject the promise, or they "do not have any impact" (or: effect) on the promise.

We can demonstrate this by using resolve once to resolve the promise, then attempting to call resolve() again or attempting to call reject():

function demo(result) {
  new Promise((resolve, reject) => {
    resolve(result); // this resolves the promise

    resolve("update!"); // this has no effect
    reject(new Error("update")); // nor does this
  }).then(value => {
    console.log(`the promise was fulfilled to ${value}`);
  }, reason => {
    console.log(`the promise was rejected with ${reason}`);
  });
}
demo(1);
demo(Promise.reject(new Error("final")));
demo(new Promise(resolve => { setTimeout(resolve, 500, "delay"); }));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks @Bergi +1 - So, basically it means that once a promise is *resolved* or *settled*, it can no longer be *resolved* or *settled*, ¿right? –  Mar 11 '23 at 23:58
  • 1
    @Coder23 Yes, a promise won't ever leave these states. – Bergi Mar 12 '23 at 00:56
1

One important property of promises is that they can only resolve once. Once a promise is resolved, any attempt to resolve it in a different way will be ignored (it has no effect on the promise's state).

For instance:

  • If a promise was fulfilled with value 3, and you try to fulfil it with value 19, it will have no effect. If you try to reject it, it will have no effect. If you try to lock it in to another promise, it will have no effect. In all these scenarios the promise will remain fulfilled with value 3.

  • If a promise was rejected with reason "error", and you try to fulfil it, it will have no effect. If you try to reject it with a different reason "another error", if will have no effect. If you try to lock it in to another promise, it will have no effect. In all these scenarios the promise will remain rejected with reason "error".

  • If a promise was locked in to another promise, and you try to fulfil it, it will have no effect. If you try to reject it, it will have no effect. If you try to lock it in to another promise than the one it is locked in to, it will have no effect. In all these scenarios the promise will remain locked in to the first promise it was locked in with.

See the definitions of the terms resolved, fulfilled, rejected, pending, and locked-in in the ECMA Script specification.

NB: A promise gets locked-in when it is resolved with another promise. As that second promise may be pending, the first promise is thus resolved, but still pending.

trincot
  • 317,000
  • 35
  • 244
  • 286