-
Notifications
You must be signed in to change notification settings - Fork 29
Interest in aligning with ES6 Promise semantics? 8000 #21
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
Comments
RXPromise tries to follow the Promises/A+ spec. Especially, regarding your question, it tries to accomplish this:
In other words, in RXPromise, if a pending promise B will be resolved with another promise A (either pending or resolved), the promise B "adopts the state" of promise A. More precisely, the set of continuations registered for A and B will execute on their respective execution context when the promise A gets resolved, and both promises now have the same state (either Cancellation will be handled as well in a similar fashion. If promise B is already cancelled when the implementation adopts the state, promise A will be cancelled as well. Likewise, when the cancellation on promise B happens after the adoption, promise B also gets cancelled and vice versa. If I take your example:
|
I believe my confusion/concern boils done to: RXPromise/RXPromise Libraries/Source/RXPromise.mm Lines 879 to 896 in 6466cec
Everything else I have encountered so far, (as I get used to the Objective-c'isms) feels good, good job. |
Well, yes. Possibly, a solution would be to have a class Another requirement is to let a resolver easily subclass a RXPromise (respectively a Deferred, it it were split). There are a few important use cases which require a subclass of RXPromise. So, splitting RXPromise into a Promise and a Deferred, should guarantee that this is still possible. Not sure why I really chose this design, perhaps since Objective-C usually likes to use conventions over, well, compiler errors. So, here the convention for a consumer is, "don't resolve a promise" ;) A couple months ago, I implemented a C++ version of a promise where I chose a to make the deferred API private, very much as an effect of a knee-jerk reaction. So, I believe this design choice has something to do with the kind of language ;) But seriously, I will investigate the options to make the deferred part a subclass and thus a "declared private" API for the consumer, unless you know a better solution. I would also prefer a "light weight" implementation relying on conventions over a "strict" implementation which could only be accomplished with a more complex class layout.
Strictly, if there is a
Thanks :) |
should mimic the ES spec's It should accept a value, a promise, or an error. When accepting a promise as it's argument the newly constructed promise's fate should be that of the promise passed as the argument.
this is part of my first example: var error = new Error();
var rejectedPromise = Promise.reject(error);
var promise = Promise.resolve(rejectedPromise)
promise.then(function(value) {
// never invoked
}, function(reason) {
reason === error // => true
}); |
Ya it would appear that in objective-c land we need only the following:
Where if ([foo class] == self) {
return foo
} else {
return [self promiseWithResult:foo];
} In theory though, wonder if we need both, resolveWithResult may be sufficient (in objective-c) |
I understand this is objective-c land, but as promises in JavaScript are now part of a formal standard, is there interest in aligning this project with those semantics?
One very specific and confusing difference is how the promises compose.
in ES6: there is no concept of a promise fulfilling with another promise, when a promise is to resolve another promise, it merely assimilates the new promises state.
Simple example:
Why is this useful?
It encourages encapsulation, and mimics synchronous programmings try/catch
example continued:
sync:
async
To summarize, these chaining and assimilation semantics encourage best practices. Although this library is clearly not JavaScript, aligning only improves mindshare on the topic.
The text was updated successfully, but these errors were encountered: