Skip to content

Instantly share code, notes, and snippets.

@olbrichj
Last active March 23, 2018 15:56
Show Gist options
  • Save olbrichj/118923f7284aac16fe3ee065dfb7bd10 to your computer and use it in GitHub Desktop.
Save olbrichj/118923f7284aac16fe3ee065dfb7bd10 to your computer and use it in GitHub Desktop.
extension Foo {
static func promise() -> Promise<FooResult> {
return PromiseWithDelegate().promise
}
}
class PromiseWithDelegate: FooDelegate {
let (promise, seal) = Promise<FooResult>.pending()
private let foo = Foo()
init() {
super.init()
retainCycle = self
foo.delegate = self // does not retain hence the `retainCycle` property
promise.ensure {
// ensure we break the retain cycle
self.retainCycle = nil
}
}
func fooSuccess(data: FooResult) {
seal.fulfill(data)
}
func fooError(error: FooError) {
seal.reject(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment