PromiseKit cancels a promise

How to cancel a promise that has not yet been fulfilled or rejected?

The documentation for PromiseKit talks about breaking a promise, but I cannot find a concrete example of how to do this.

Considering:

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 (the user enters some text in the search bar), I want to cancel and cancel the currentOperation , starting a new promise.

+5
source share
1 answer

To cancel a promise, you must reject it with any type of error that conforms to the CancellableError protocol. Thus, any catch block with the policy parameter set to allErrorsExceptCancellation will skip the error.

If you need CancellablePromise, you can subclass Promise and implement the cancel () function, which will be rejected upon CancellableError when called. Here is a minimal implementation:

https://gist.github.com/EfraimB/918eebdf7dd020801c72da1289c8d797

+1
source

All Articles