Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allSettled to RSVP #113

Merged
merged 1 commit into from
Jan 4, 2014
Merged

Add allSettled to RSVP #113

merged 1 commit into from
Jan 4, 2014

Conversation

decasia
Copy link
Contributor

@decasia decasia commented Sep 17, 2013

The existing RSVP.all method implements essentially a fail-fast method of resolving an array of promises. But sometimes you don't want the fail-fast behavior and instead want to be able to inspect the final states of a bunch of promises, whether they succeed or fail. One use case I've encountered is in doing validation handling when saving a bunch of related records in Ember — if you want to be able to trigger client-side logic based on the responses to all the record states, RSVP.all is not really effective, because it doesn't continue after the first one.

So this PR would add another method RSVP.allSettled that transforms an array of promises into a promise for an array of their states. These states will take one of the following formats:

{ state: 'fulfilled', value: value }
  or
{ state: 'rejected', reason: reason }

These result states are taken from a similar allSettled function in the Q library (compare also the allSettled tests). And for coherence with the existing RSVP codebase, I've kept the implementation, function and variable names, etc, as close as possible to RSVP.all for the time being.

I've never sent a GitHub PR before, so I'd obviously be grateful for any tips or suggestions!

@domenic
Copy link
Contributor

domenic commented Sep 17, 2013

+1

@mmun
Copy link
Collaborator

mmun commented Sep 17, 2013

@domenic: Why allSettled vs allResolved? I'm not sure if the reasoning in kriskowal/q#257 is relevant to RSVP, I was unable to follow that thread and the Promises/A+ spec doesn't mention "settled" promises.

I also had a quick discussion with @twokul regarding the proposed API. What if allSettled fulfilled with an array of values if all the dependent promises fulfill, but reject with with an array of snapshots in the event that any of the dependent promises reject? This would lead to less code in the fulfilled case.

@domenic
Copy link
Contributor

domenic commented Sep 17, 2013

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

What if allSettled fulfilled with an array of values if all the dependent promises fulfill, but reject with with an array of snapshots in the event that any of the dependent promises reject? This would lead to less code in the fulfilled case.

How do you propose to distinguish the case of RSVP.allSettled([RSVP.resolve({ state: "rejected", reason: 1 })]) from the case of RSVP.allSettled([RSVP.reject(1)])?

@mmun
Copy link
Collaborator

mmun commented Sep 17, 2013

@domenic The first would fulfill with [{ state: "rejected", reason: 1 }] and the second would reject with [{ state: "rejected", reason: 1 }] or have I misunderstood?

Thanks for the link. I really appreciate that you've taken the time to really nail down the terminology!

@domenic
Copy link
Contributor

domenic commented Sep 17, 2013

No, allSettled never rejects. Creating some kind of composite-Error object is not the business promise libraries want to be in, or at least Q and DOM Promises do not.

@mmun
Copy link
Collaborator

mmun commented Sep 17, 2013

Gotcha. Thanks.

@stefanpenner
Copy link
Collaborator

i'll be doing a RSVP push this weekend. If u can rebase this that would be great.

@domenic this still look good?

@decasia
Copy link
Contributor Author

decasia commented Nov 22, 2013

@stefanpenner let me know if it needs further updating!

@@ -2,6 +2,7 @@ import { EventTarget } from "./rsvp/events";
import { Promise } from "./rsvp/promise";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol this file is crying for some export * from "x" love. I wonder if es6-module-transpiler supports that yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefanpenner
Copy link
Collaborator

i believe this is now referred to as settled (@domenic can you confirm?)

@decasia do you have time to update this PR?

@decasia
Copy link
Contributor Author

decasia commented Dec 24, 2013

I can update it on Jan 2nd -- believe it or not, that's the next time I'll be at a keyboard! Sorry about the delay, will get to this then if you can wait that long.

On Dec 24, 2013, at 12:20 AM, Stefan Penner [email protected] wrote:

i believe this is now referred to as settled

@decasia do you have time to update this PR?


Reply to this email directly or view it on GitHub.

@domenic
Copy link
Contributor

domenic commented Dec 24, 2013

@stefanpenner nope, it's still allSettled in Q. Looks like when has it under the name settle.

@stefanpenner
Copy link
Collaborator

@decasia no worries, enjoy your time free of a keyboard!

@decasia
Copy link
Contributor Author

decasia commented Jan 3, 2014

OK, I've updated the PR. It now uses shared helpers for isArray and isNonThenable, and adds docs and function specifications. For good measure, I updated some variable names to more closely match what's now in all, and I improved the tests so that they check the case of a constituent promise rejecting.

How does this look, @stefanpenner? and happy new year folks!

// Note that for the second item, reason.message will be "2", and for the
// third item, reason.message will be "3".
}, function(error) {
// Code here never runs because allSettled never rejects.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code will run if allSettled failed. e.g. was passed an incorrect type.

@stefanpenner
Copy link
Collaborator

i believe with those last 2 comments, we should be good.

@decasia
Copy link
Contributor Author

decasia commented Jan 4, 2014

how about now?

Stefan Penner mailto:[email protected]
January 3, 2014 6:02 PM

i believe with those last 2 comments, we should be good.


Reply to this email directly or view it on GitHub
#113 (comment).

@stefanpenner
Copy link
Collaborator

@decasia looks fantastic, thanks for the hard work!

stefanpenner added a commit that referenced this pull request Jan 4, 2014
@stefanpenner stefanpenner merged commit e94e4cd into tildeio:master Jan 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants