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
promisehas already been resolved, the receiver will be resolved immediately. Otherwise the receiver will wait untilpromiseis resolved and resolve to the same result.If the receiver is cancelled, it will also propagate the cancellation to
promisethe same way that a child promise does. If this is not desired, then either useresolve(with: promise.ignoringCancel())orpromise.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
contextThe context that the callback is invoked on.
callbackThe 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
onRequestCancelhandler.Declaration
Swift
public var hasRequestedCancel: Bool { get }
-
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
ValueandErrorpassed to the closure arenilthe promise is rejected withPromiseCallbackError.apiMismatch. If they’re both non-nilthis 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?) -> VoidParameters
isCancelErrorAn 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.
-
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>)
View on GitHub
Resolver Structure Reference