Structures

The following structures are available globally.

  • DelayedPromise is like a Promise but it doesn’t invoke its callback until the .promise variable is accessed.

    The purpose of DelayedPromise is to allow functions to return calculations that aren’t performed if they’re not needed.

    Example:

    func getUserInfo() -> (name: String, avatar: DelayedPromise<UIImage,Error>) {
        
    }
    
    let (name, avatar) = getUserInfo()
    nameLabel.text = name
    avatar.promise.then { [weak self] (image) in
        self?.imageView.image = image
    }
    
    See more

    Declaration

    Swift

    public struct DelayedPromise<Value, Error> : Equatable
  • A Promise is a construct that will eventually hold a value or error, and can invoke callbacks when that happens.

    Example usage:

    Promise(on: .utility) { resolver in
        let value = try someLongComputation()
        resolver.fulfill(with: value)
    }.then(on: main) { value in
        self.updateUI(with: value)
    }.catch(on: .main) { error in
        self.handleError(error)
    }
    

    Promises can also be cancelled. With a Promise object you can invoke .requestCancel(), which is merely advisory; the promise does not have to actually implement cancellation and may resolve anyway. But if a promise does implement cancellation, it can then call resolver.cancel(). Note that even if the promise supports cancellation, calling .requestCancel() on an unresolved promise does not guarantee that it will cancel, as the promise may be in the process of resolving when that method is invoked. Make sure to use the invalidation token support if you need to ensure your registered callbacks aren’t invoked past a certain point.

    Note

    If a registered callback is invoked (or would have been invoked if no token was provided) it is guaranteed to be released on the context. This is important if the callback captures a value whose deallocation must occur on a specific thread (such as the main thread). If the callback is not invoked (ignoring tokens) it will be released on whatever thread the promise was resolved on. For example, if a promise is fulfilled, any callback registered with .then(on:token:_:) will be released on the context, but callbacks registered with .catch(on:token:_:) will not. If you need to guarantee the thread that the callback is released on, you should use .always(on:token:_:) or one of the .mapResult(on:token:_:) variants.
    See more

    Declaration

    Swift

    public struct Promise<Value, Error>
    extension Promise: Equatable
  • A type that can be used to cancel a promise without holding onto the full promise.

    In particular, this acts like a weak reference, allowing for cancelling the promise without creating a retain cycle. Promise retain cycles normally break themselves anyway when the promise is resolved, but a misbehaving promise body may drop the resolver without ever resolving the promise. If the Promise has no more references to it this automatically cancels the promise, but a retain cycle prevents this.

    This is returned from Promise.cancellable.

    See more

    Declaration

    Swift

    public struct PromiseCancellable
  • An invalidation token that can be used to cancel callbacks registered to a Promise.

    See more

    Declaration

    Swift

    public struct PromiseInvalidationToken : CustomStringConvertible, CustomDebugStringConvertible
    extension PromiseInvalidationToken: Hashable
  • A Promise adapter that automatically applies a PromiseInvalidationToken.

    This exposes the same methods as Promise but it always passes a given PromiseInvalidationToken to the underlying promise.

    Important

    TokenPromise automatically cancels any returned child Promises when the PromiseInvalidationToken is invalidated. The only exception is Promises returned from tap() or tap(on:_:), as these methods do not propagate cancellation.

    A TokenPromise is created with the method Promise.withToken(_:). The wrapped Promise can be accessed with the .inner property.

    Example

    methodReturningPromise()
        .withToken(promiseToken)
        .then({ (value) in
            // handle value
        }).catch({ (error) in
            // handle error
        })
    
    See more

    Declaration

    Swift

    public struct TokenPromise<Value, Error>
    extension TokenPromise: Equatable