ResultPromise

public class ResultPromise<T, E> where E : Error

ResultPromise is a minimal Promise implementation that wraps a Result<T, E> instance. It supports the fulfil, await, then and thenAsync combinators, facilitating a fluid sequencing of computations that produce either a Result (then) or a ResultPromise instance (thenAsync). (The reject combinator is redundant, as failure is handled by the embedded Result.)

  • Fulfil this promise with result.

    Declaration

    Swift

    public func fulfil(with result: Result<T, E>)

    Parameters

    result

    A Result<T, E> that describes the result.

  • Execute the observer closure when the promise is fulfilled.

    Declaration

    Swift

    public func await(with observer: @escaping (Result<T, E>) -> Void)

    Parameters

    observer

    A closure that takes a Result instance and returns nothing.

  • Produce a new ResultPromise by flatMap‘ing the supplied function over the embedded Result.

    Declaration

    Swift

    public func then<U>(_ f: (@escaping (T) -> Result<U, E>)) -> ResultPromise<U, E>

    Parameters

    f

    A closure (T) -> Result<U, E>.

    Return Value

    A new ResultPromise<U, E> instance, where the embedded Result<U, E> is the result of flatMap‘ing f over the Result<T, E> embedded in this instance.

  • Produce a new ResultPromise by applying the supplied function to the value contained in the embedded Result instance.

    Declaration

    Swift

    public func thenAsync<U>(_ f: (@escaping (T) -> ResultPromise<U, E>)) -> ResultPromise<U, E>

    Parameters

    f

    A closure (T) -> Result<U, E>.

    Return Value

    A new ResultPromise<U, E> instance.