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 features for handling values #59

Closed
wants to merge 3 commits into from
Closed

Add features for handling values #59

wants to merge 3 commits into from

Conversation

mjackson
Copy link

It is very useful when writing a function that returns a promise to be able to return a value instead, in case you already have the value available. This way the caller can always treat the return value as a promise. So I've added a few "static methods" to the RSVP object that facilitate easier handling of already-resolved values.

RSVP.when

This function is the static equivalent of Promise.prototype.then. You can pass it a promise or a value, along with the usual done and fail callbacks.

RSVP.when(promise, done, fail);
RSVP.when(value, done); // Always resolves.

RSVP.resolve/reject

These are convenience methods for generating an already resolved/rejected promise for a given value.

RSVP.resolve(value).then(done);
RSVP.reject(value).then(null, fail);

RSVP.all

I also made a slight change to RSVP.all which goes along with these changes. It now knows how to resolve values as well as promises.

RSVP.all([ value, promise ]).then(done, fail);

@mjackson
Copy link
Author

Oops. Just noticed that this pull request partially duplicates some work that @domenic did in #54. The only duplication is the part about RSVP.all being able to handle values as well as promises.

I think it just goes to show how useful this functionality can be. :)

@mjackson
Copy link
Author

Closing this because the most important piece was already merged, albeit not from my work, and on second thought I don't think the other methods fit very nicely into the minimalistic approach of rsvp.js.

@domenic
Copy link
Contributor

domenic commented Mar 17, 2013

I think these are important. RSVP needs to fix its confusion between resolve and fulfill, but once it does, RSVP.resolve will be really nice. I've opened #60 for that. RSVP.reject on top of that would be helpful.

I'm not as sure about RSVP.when, since RSVP.when(x, f, r) can just be replaced with RSVP.resolve(x).then(f, r).

@mjackson
Copy link
Author

My version of RSVP.resolve was actually more of a RSVP.fulfill. Your version of RSVP.resolve is just like my RSVP.when was. I was just using RSVP's existing terminology instead of using "resolve" and "fulfill" as they are defined in Promises A+. So I think we're in agreement.

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.

2 participants