Understanding Promises

Hey guys, I don't quite understand this, it seems quite cyclical? It seems to be defining a term using the term itself?
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.
Original article: https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md
10 Replies
gunter
gunterOP3mo ago
And there's a distinction between states and fates? I get what states are: the current condition the thing is in But fates?
ἔρως
ἔρως3mo ago
that's a very wordy explanation for something so simple but basically, it means that there's 2 things that are pre-destined to happen
gunter
gunterOP3mo ago
I don't understand.. So what does fates - resolved and unresolved - mean again?
ἔρως
ἔρως3mo ago
it's using the meaning of something pre-destined to happen
gunter
gunterOP3mo ago
Ah If you don't mind, could we work through some examples? It's been really long since I've worked with JavaScript and I'm just getting back at it 😅
ἔρως
ἔρως3mo ago
im playing overwatch, but sure
13eck
13eck3mo ago
I'm a bit confused as to their use of terminology, but basically a promise is unresolved if it's pending. A fulfilled or rejected promise is resolved. For example. Say you use fetch(https://example.com/api/user?id=1) It's pending while the HTTP request is in-flight. While pending it's unresolved. The HTTP call succeeds, so the promise is now resolved to a fulfilled state. Say, instead, you misspelled the URL: fetch(https://exmple.com/api/user?id=1). When the DNS fails to locate the website the promise is resolved to a rejected state.
Ganesh
Ganesh3mo ago
Resolved promise can be pending, rejected or fulfilled all three if it's resolution is handled by another promise
Ganesh
Ganesh3mo ago
I think when it is not dependent on another promise for it's resolution then it is unresolved if the state is pending
const somePromise = new Promise((resolveOuter) => {
resolveOuter(
new Promise((resolveInner) => {
setTimeout(resolveInner, 5000);
}),
);
});

somePromise
const somePromise = new Promise((resolveOuter) => {
resolveOuter(
new Promise((resolveInner) => {
setTimeout(resolveInner, 5000);
}),
);
});

somePromise
running this code in browser for example shows somePromise as pending and after 5 seconds it is fullfilled by the inner promise. but between these 5 seconds the outer promise is set to be resolved by the inner promise and it can't do reject or resolve by it's own, it is locked in with the inner promise. at least that's what i get from it

Did you find this page helpful?