Resolver

public struct Resolver

A Resolver is used to fulfill, reject, or cancel its associated Promise.

  • Fulfills the promise with the given value.

    If the promise has already been resolved or cancelled, this does nothing.

    Declaration

    Swift

    public func fulfill(with value: Value)
  • Rejects the promise with the given error.

    If the promise has already been resolved or cancelled, this does nothing.

    Declaration

    Swift

    public func reject(with error: Error)
  • Cancels the promise.

    If the promise has already been resolved or cancelled, this does nothing.

    Declaration

    Swift

    public func cancel()
  • Resolves the promise with the given result.

    If the promise has already been resolved or cancelled, this does nothing.

    Declaration

    Swift

    public func resolve(with result: PromiseResult<Value, Error>)
  • Resolves the promise with another promise.

    If promise has already been resolved, the receiver will be resolved immediately. Otherwise the receiver will wait until promise is resolved and resolve to the same result.

    If the receiver is cancelled, it will also propagate the cancellation to promise the same way that a child promise does. If this is not desired, then either use resolve(with: promise.ignoringCancel()) or promise.always(on: .immediate, resolver.resolve(with:)).

    Declaration

    Swift

    public func resolve(with promise: Promise<Value, Error>)
  • Registers a block that will be invoked if requestCancel() is invoked on the promise before the promise is resolved.

    If the promise has already had cancellation requested (and is not resolved), the callback is invoked on the context at once.

    Note

    If you register the callback for a serial queue and resolve the promise on that same serial queue, the callback is guaranteed to not execute after the promise is resolved.

    Declaration

    Swift

    public func onRequestCancel(on context: PromiseContext, _ callback: @escaping (Resolver) -> Void)

    Parameters

    context

    The context that the callback is invoked on.

    callback

    The callback to invoke.

  • Returns whether the promise has already been requested to cancel.

    This can be used when a promise init method does long-running work that can’t easily be interrupted with a onRequestCancel handler.

    Declaration

    Swift

    public var hasRequestedCancel: Bool { get }

Available where Error == Swift.Error

  • Resolves the promise with the given result.

    If the promise has already been resolved or cancelled, this does nothing.

    Declaration

    Swift

    public func resolve<E>(with result: PromiseResult<Value, E>) where E : Error
  • Convenience method for handling framework callbacks.

    This method returns a closure that can be passed to a framework method as a callback in order to resolve the promise. It takes an optional parameter that can be used to determine when the error represents cancellation. For example:

    geocoder.reverseGeocodeLocation(location, completionHandler: resolver.handleCallback(isCancelError: { CLError.geocodeCanceled ~= $0 }))
    

    If both the Value and Error passed to the closure are nil the promise is rejected with PromiseCallbackError.apiMismatch. If they’re both non-nil this should be considered an error, but the promise will be fulfilled with the value and the error will be ignored.

    Declaration

    Swift

    public func handleCallback(isCancelError: @escaping (Error) -> Bool = { _ in false }) -> (Value?, Error?) -> Void

    Parameters

    isCancelError

    An optional block that can be used to indicate that specific errors represent cancellation.

    Return Value

    A closure that can be passed to a framework method as a callback.

Available where Error: Swift.Error

  • Resolves the promise with the given result.

    If the promise has already been resolved or cancelled, this does nothing.

    Declaration

    Swift

    public func resolve(with result: Result<Value, Error>)