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
Change Promise properties: isPending, isFulfilled, isRejected from In… #31
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
|
Hi @oxview, could you provide any more info on why you believe exposing those properties is necessary? |
Hi @shoumikhin, I have something like this: var promise: Promise<Void>
func connect() -> Promise<Void> {
self.promise = Promise<Void>.pending()
// connect to host ...
return self.promise
}
func socket(didConnectToHost host: String) {
self.promise.fulfill(())
}
func socket(didDisconnectFromHost host: String) {
if self.promise.isPending {
self.promise.reject("some error")
// we are sure that we was not connected, self.promise was not fullfiled/rejected
// do 1 logic ...
} else {
// do 2 logic ...
}
} |
@shoumikhin Moving from other |
Thanks you for the details! Are you using some kind of a networking library that provides a delegate to notify about connections? Wonder if there's any API which provides a completion handler instead, which you could then wrap with If no, and being a delegate is the only way, how about a nullable var promise: Promise<Void>?
func connect() -> Promise<Void> {
if promise != nil {
promise?.reject(MyError.abandoned)
promise = nil
}
let newPromise = Promise<Void>.pending()
promise = newPromise
// connect to host ...
return newPromise
}
func socket(didConnectToHost host: String) {
promise?.fulfill(())
promise = nil
}
func socket(didDisconnectFromHost host: String) {
if promise != nil {
promise?.reject(MyError.disconnected)
promise = nil
// we are sure that we was not connected, self.promise was not fullfiled/rejected
// do 1 logic ...
} else {
// do 2 logic ...
}
} Just make sure the users of such promise won't introduce retain cycles with something like: object.connect().then { _ in
object.doSomethingElse()
// object is now captured by the block which is captured by the promise returned from connect(), which in turn is owned by the object
// although, the retain cycle will get broken once the promise gets resolved in either delegate call and nullified
} Anyhow, checking the state of a promise with |
Thank you for you advise and I am absolutely agree with |
Change Promise properties:
isPending, isFulfilled, isRejected
from Internal to Public