PromiseKit cancel a promise
In order to cancel a promise you have to reject it with any error type that conforms to CancellableError
protocol. This way any catch block with policy
parameter set to allErrorsExceptCancellation
will let the error pass through.
If you need a CancellablePromise you may subclass Promise and implement a cancel() function that will reject with a CancellableError
when called. Here is a minimal implementation:
https://gist.github.com/EfraimB/918eebdf7dd020801c72da1289c8d797
UPDATE:
Here is an update for the new PromiseKit version (6.4.1)
https://gist.github.com/EfraimB/3ac240fc6e65aa8835df073f68fe32d9
Maybe my library CancellablePromiseKit is useful for you: https://github.com/johannesd/CancellablePromiseKit
It allows you to define a CancellablePromise similarly to how you define an ordinary Promise, with the addition of a cancel block. In that block, you write the code for cancelling the underlying task. The promise can then be cancelled by calling cancellablePromise.cancel() from the outside.
let cancellablePromise = CancellablePromise<String> { resolver in
let currentOperation = client.load(skip: skip, query: nil)
currentOperation.completion = { (value, error) in
resolver.resolve(value, error)
}
return {
// The cancel block
currentOperation.stop()
}
}
The lib also overloads when and race to cancel tasks automatically.