Skip to content

Commit

Permalink
Fix recover fail with cancelled promise
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Sep 1, 2017
1 parent fa61b34 commit 5040133
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
16 changes: 1 addition & 15 deletions DemoApp/ViewController.swift
Expand Up @@ -27,21 +27,7 @@ class ViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let invalidator: InvalidationToken = InvalidationToken()

let x = async(token: invalidator, { st -> String in
Thread.sleep(forTimeInterval: 2.0)
if st.isCancelled {
print("cancelled")
} else {
print("opened")
}
return "x"
}).then { x in
print("x")
}
invalidator.invalidate()
}

}

7 changes: 4 additions & 3 deletions Sources/Hydra/Promise+Retry.swift
Expand Up @@ -48,11 +48,12 @@ public extension Promise {
return Promise<Value>(rejected: PromiseError.invalidInput)
}

var innerPromise: Promise<Value>? = nil
var remainingAttempts = attempts
// We'll create a next promise which will be resolved when attempts to resolve self (source promise)
// is reached (with a fulfill or a rejection).
let nextPromise = Promise<Value>(in: self.context, token: self.invalidationToken) { (resolve, reject, operation) in
let innerPromise = self.recover(in: self.context) { [unowned self] (error) -> (Promise<Value>) in
innerPromise = self.recover(in: self.context) { [unowned self] (error) -> (Promise<Value>) in
// If promise is rejected we'll decrement the attempts counter
remainingAttempts -= 1
guard remainingAttempts >= 1 else {
Expand Down Expand Up @@ -83,8 +84,8 @@ public extension Promise {
let onCancel = Observer.onCancel(self.context, operation.cancel)

// Observe changes from source promise
innerPromise.add(observers: onResolve, onReject, onCancel)
innerPromise.runBody()
innerPromise?.add(observers: onResolve, onReject, onCancel)
innerPromise?.runBody()
}
nextPromise.runBody()
return nextPromise
Expand Down

0 comments on commit 5040133

Please sign in to comment.