promises-unwrapping | The ES6 promises spec , as per September 2013 TC39 meeting | Runtime Evironment library
kandi X-RAY | promises-unwrapping Summary
kandi X-RAY | promises-unwrapping Summary
The ES6 promises spec, as per September 2013 TC39 meeting
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Build a Promise .
promises-unwrapping Key Features
promises-unwrapping Examples and Code Snippets
Community Discussions
Trending Discussions on promises-unwrapping
QUESTION
What is the difference between Promise.any()
and Promise.race()
, and how do they get used differently?
From MDN,
Also, unlike Promise.race(), which returns the first settled value, this method returns the first resolved value. This method will ignore all rejected promises up until the first promise that resolves.
So that brings me to, the difference between resolved and settled. Which then brought me to the MDN promises page, which then brought me to States and Fates
Being settled is not a state, just a linguistic convenience.
So we have Promise.any
and Promise.race
for linguistic convenience? i.e. there is no difference. Another instance of this equality, is "A promise whose fate is unresolved is necessarily pending." and "We say that a promise is settled if it is not pending, i.e. if it is either fulfilled or rejected.".
So if a promise is resolved, it is not unresolved, so it is not pending. So then, if its not pending, its settled. So resolved === settled.
...ANSWER
Answered 2020-May-11 at 16:17Promise.race
and Promise.any
do different things:
Promise.race
is settled as soon as any of the promises you feed it settle, whether they are fulfilled or rejected.
Promise.any
is settled as soon as any of the promises you feed it is fulfilled or they are all rejected, in which case it's rejected with an AggregateError
.
The chief differences are:
race
's promise is rejected when the first promise you give it is rejected;any
doesn't, because another promise may be fulfilled instead.any
's promise's rejection reason will be anAggregateError
, butrace
's rejection reason will be the rejection reason from the promise that was rejected.
So if you pass them both an array of two promises, and one of the promises is rejected, then afterward the other promise is fulfilled, the promise from Promise.race
will be rejected (because the first promise to settle was rejected) and the promise from Promise.any
will be fulfilled (because although the first promise was rejected, the second was fulfilled). E.g.:
QUESTION
I have a script that sets a set number of usernames, and then for each username, runs a fetch request to twitches API endpoint to get the Json information for each user. I then wanted to put that information into an array for later use, and found out I had to use promises, which I never worked with before. I read through this: https://developers.google.com/web/fundamentals/primers/promises and this: https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md and I read Felix Kling response here: How do I return the response from an asynchronous call? and I'm still having trouble understanding exactly what goes where and what does what. This is what I have so far:
...ANSWER
Answered 2018-Apr-19 at 01:53do something like this instead:
QUESTION
In nearly all the documentation I read, promises are constructed with an executor function whose first argument is a function named "resolve":
...ANSWER
Answered 2018-Feb-01 at 07:13Here's one possible explanation.
A promise ultimately ends up fulfilled or rejected and it can be send directly to one of those states by fulfilling with a value or rejecting with a reason.
But, a promise can also be tied to another thenable and it will then track that thenable. If that thenable ends up fulfilled, this promise will be fulfilled. If that other thenable ends up rejected, this promise will be rejected.
So, with the executor, you can actually do three things:
- Fulfill to a value (that is not a thenable).
- Reject to a reason
- Resolve to a thenable which will be tracked and whose ultimate disposition will be inherited by this promise.
Ahhh, but you don't need three functions to implement all this. You can do it with just two. #1 and #3 can use the same function and just vary what they pass to it (either a thenable or a non-thenable value). So, you need a verb that captures both #1 and #3. Folks have generally picked resolve
as that verb. It is resolved to either a value or thenable.
So, you get new Promise(function(resolve, reject) {...});
. You have to pick some word for the first function. Most people writing documentation picked resolve()
. If you resolve to a non-thenable value, you get a fulfilled promise. If you resolve to a thenable, this promise will track that thenable and take on it's eventual state.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install promises-unwrapping
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page