How do I cancel a promise that has been neither fulfilled or rejected yet?
The documentation for PromiseKit talks about cancelling a promise, but I can't find a specific example of how to do this.
Given:
currentOperation = client.load(skip: skip, query: nil)
currentOperation!.then { (items) in
self.processItems(items: items, skip: skip, query: query)
}.catch { (error) in
print("failed to load items - just retrying")
self.loadIfNeeded(skip: skip, query: query, onlyInStock: onlyInStock)
}
If the query changes (user enters some text in the search bar) I want to cancel and discard the currentOperation
, starting a new promise.
In PromiseKit 7, use func cancellize()
to convert a Promise
or Guarantee
into a promise that can be cancelled:
currentOperation = client.load(skip: skip, query: nil)
let currentOperationCancellable = currentOperation!.then { (items) in
self.processItems(items: items, skip: skip, query: query)
}.cancellize()
currentOperationCancellable.catch { (error) in
print("failed to load items - just retrying")
self.loadIfNeeded(skip: skip, query: query, onlyInStock: onlyInStock)
}
Use func cancel()
or func cancel(with:)
to cancel a cancellable Promise
or Guarantee
:
currentOperationCancellable.cancel()
currentOperationCancellable.cancel(with: NSError(domain: "", code: 0, userInfo: nil))