Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding Promise.cancellingImmediatelyUponRequest() #28

Open
lilyball opened this issue Nov 24, 2018 · 0 comments
Open

Consider adding Promise.cancellingImmediatelyUponRequest() #28

lilyball opened this issue Nov 24, 2018 · 0 comments
Labels
enhancement New feature or request

Comments

@lilyball
Copy link
Owner

We might want to add a function like

extension Promise {
    /// Returns a new `Promise` that cancels immediately when requested.
    ///
    /// The new promise adopts the value of the receiver, unless the new promise
    /// is requested to cancel first. When `requestCancel()` is invoked the new
    /// promise will cancel itself immediately in addition to propagating the
    /// cancel upwards. This means the resulting promise will support
    /// cancellation even if the upstream promise doesn't.
    ///
    /// - Note: This method means the resulting promise can resolve to the
    /// `.cancelled` state even if the upstream promise is fulfilled or
    /// rejected.
    public func cancellingImmediatelyUponRequest() -> Promise {
        return Promise(on: .immediate, { (resolver) in
            let upstream = self.inspect(on: .immediate, { (result) in
                resolver.resolver(with: result)
            })
            resolver.onCancelRequested(on: .immediate) { [upstream=upstream.cancellable] (resolver) in
                resolver.cancel()
                upstream.requestCancel()
            }
        })
    }
}

It's not clear yet if this function is useful enough to justify its inclusion in the core library.

@lilyball lilyball added the enhancement New feature or request label Nov 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant