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
And there's a distinction between states and fates?
I get what states are: the current condition the thing is in
But fates?
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
I don't understand..
So what does fates - resolved and unresolved - mean again?
it's using the meaning of something pre-destined to happen
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 😅
im playing overwatch, but sure
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.Resolved promise can be pending, rejected or fulfilled all three if it's resolution is handled by another promise
MDN Web Docs
Promise - JavaScript | MDN
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
I think when it is not dependent on another promise for it's resolution then it is unresolved if the state is pending
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