Comments (18)
@ljharb I am not too concerned about the name itself. It can be .settle
on the promise. It's also not about it being a prototype method. It's more about it acting on a single promise rather than an array of promises.
from proposal-promise-allsettled.
Very interesting issue. My intuition is that allSettled is still useful and convenient, even if we additionally added reflect() or similar as a lower-level primitive with slightly broader applicability. But, the fact that Bluebird deprecated their allSettled counterpart is concerning. @benjamingr, could you say more about this?
Champions, I think this is worth raising for committee discussion when transitioning to stage 3. As I said, I'd be supportive of moving ahead with allSettled and considering this an add-on, but it'd be good to ensure the committee agrees.
from proposal-promise-allsettled.
@rpamely sorry, I missed your comment there. Basically, you would reflect
whenever an API you are calling failing something does not necessarily mean failure.
Here are some examples and use cases:
// network request
const hostReachability = await fetch('http://might-not-be-reachable.com').reflect();
const isHostReachable = hostReachability.status === 'fulfilled';
// timer that might be cancelled
const timerDone = await setTimeout(4000, token).reflect();
if (timerDone.status === 'rejected') { // cancelled, but not an error
}
// in Node.
const safeExists = await fs.readFile('./mightNotExist').reflect();
if (safeExists.status === 'fulfilled') { // the file existed and I read it
} else { // file not in cache
}
Of course all of these can be handled with writing our own reflect
(it's a 2 LoC polyfill already) or by wrapping allSettled
and destructuring the array - again, not a huge deal - just a worse off API. After all .allSettled
is a 2 line polyfill itself anyway.
from proposal-promise-allsettled.
Thanks a lot for this @benjamingr. This has given me a much clearer understanding of when reflect is useful. I agree with you that there is place for both of these features. I think allSettled is a common enough case that it deserves its own function.
from proposal-promise-allsettled.
Sure, having co-maintained the most popular promise library I have several. You're also welcome to check the threads from bluebird discussing this and why we deprecated .settle in favor of reflect.
from proposal-promise-allsettled.
I see the meeting notes mention less adoption. I have actually seen some adoption of reflect
over .settle
in multiple places. I'm not sure how common it is but the three other common ones I've seen are:
raceSettled
where we want to await the first from an array of promises and not look at it as a rejection.propsSettled
where we want to wait for properties of an object..reflect()
to protect against throwing APIs.
There are more common ones - but these were the most common I saw.
I actually don't feel too strongly about it to be honest. It just seems odd to pick the less composing abstraction and to have to do .allSettled([p]).then(([v]) => v)
in order to do it for a single promise.
from proposal-promise-allsettled.
@ljharb it most certainly didn't originate in bluebird - I'm just sad you're considering the API we moved away from
from proposal-promise-allsettled.
And to be extra clear - I'm not sure we shouldn't do allSettled
. I can't block this proposal, but if I could I still wouldn't. Spec work is full of compromises and I wouldn't be too bothered by a language that does modulu between strings containing a 'slightly worse off' primitive.
Promises themselves were full of compromises and were still a huge win for the language.
from proposal-promise-allsettled.
At today’s TC39 meeting, there was consensus to proceed with Promise.allSettled
since it carries its own weight, regardless of whether or not we decide to add reflect
as a separate proposal.
from proposal-promise-allsettled.
The decision process wasn't so thorough but we always liked that decision petkaantonov/bluebird#346 (comment)
from proposal-promise-allsettled.
Reflect is a place methods matching Proxy traps are stored; I’m not sure the name makes sense given the builtin.
Are you suggesting a prototype method in general, that gives a promise for the result object?
from proposal-promise-allsettled.
@benjamingr do you have realistic use cases for the above scenarios you listed?
from proposal-promise-allsettled.
I think this is also useful for e.g. Array#map
. A function call may "result" in a thrown exception, or a return value. I don't like naming this specific to Promise personally. Consider {throw: any} | {return: any}
instead? This generalises it for use in any monad, not just Promise.
function f(x) {
if (x) {
throw new Error();
} else {
return "foo";
}
}
capture(() => f(true))
🠚 {throw: theErrorObject}
capture(() => f(false))
🠚 {return: "foo"}
from proposal-promise-allsettled.
It seems like this is moving forward as allSettled
, it's not a huge shame - but it is still a shame to see others repeating our mistakes.
from proposal-promise-allsettled.
@benjamingr i don't think the term "settled" originates in Bluebird, fwiw.
from proposal-promise-allsettled.
@domenic honestly this isn't a huge deal - there are many cases where reflect
applies and allSettled
doesn't - similarly to how we had Promise.map
in libraries but people just do Promise.all
with array#map
.
I think it's generally preferable to add things that compose well with each other - I'm sure we'll see Promise.allSettled([p]).then(([result]) => ...
which is just a worse API.
from proposal-promise-allsettled.
Passing one item to allSettled will mean you will immediately have to branch on the status for one item to read the value. Why not just use .then().catch() in this case? Same for raceSettled. When the result is one item I don't see what you would need reflect for. Would you mind elaborating please @benjamingr?
from proposal-promise-allsettled.
@domenic honestly this isn't a huge deal - there are many cases where
reflect
applies andallSettled
doesn't - similarly to how we hadPromise.map
in libraries but people just doPromise.all
witharray#map
.I think it's generally preferable to add things that compose well with each other - I'm sure we'll see
Promise.allSettled([p]).then(([result]) => ...
which is just a worse API.
I disagree with your example of Promise.map()
, people don't use Promise.map
may just because it is not an ECMA standard making it hard to switch between libraries, or many other reasons, which doesn't apply to current matter of Promise.allSettled()
.
Other than that, I don't see why we can't have both, .allSettled()
and .reflect()
.
from proposal-promise-allsettled.
Related Issues (20)
- Advance to stage 3 HOT 10
- Advance to stage 4 HOT 2
- gh-pages link is dead
- The return type is not very friendly to parameter destructuring HOT 4
- Initial spec review HOT 1
- Clarify abrupt completions HOT 8
- Clarify Completions HOT 1
- Clarify [[AlreadyCalled]] HOT 3
- Clarify CreateDataProperty with string values HOT 2
- Clarify the need for `Function#length` notes HOT 4
- Add a second parameter to indicate is-resolved for each member of the array HOT 4
- Misspelling "fullfilled" HOT 16
- Compatibility with existing Promise subclasses HOT 7
- Missleading sentence HOT 1
- Editorial nit: "constructor is a constructor function" HOT 1
- Editorial nit: add more !s
- Test262 coverage note HOT 1
- A flag in addition to (or rather than) `status`? HOT 7
- Poisoned `then` property rejecting the returned promise HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-promise-allsettled.