Promise

public final class Promise<T> : Thenable, CatchMixin
extension Promise: CustomStringConvertible
extension Promise: CustomDebugStringConvertible

A Promise is a functional abstraction around a failable asynchronous operation.

See

Thenable
  • Initialize a new fulfilled promise.

    We do not provide init(value:) because Swift is “greedy” and would pick that initializer in cases where it should pick one of the other more specific options leading to Promises with T that is eg: Error or worse (T->Void,Error->Void) for uses of our PMK < 4 pending initializer due to Swift trailing closure syntax (nothing good comes without pain!).

    Though often easy to detect, sometimes these issues would be hidden by other type inference leading to some nasty bugs in production.

    In PMK5 we tried to work around this by making the pending initializer take the form Promise(.pending) but this led to bad migration errors for PMK4 users. Hence instead we quickly released PMK6 and now only provide this initializer for making sealed & fulfilled promises.

    Usage is still (usually) good:

    guard foo else {
        return .value(bar)
    }
    

    Declaration

    Swift

    public static func value(_ value: T) -> Promise<T>
  • Initialize a new rejected promise.

    Declaration

    Swift

    public init(error: Error)
  • Initialize a new promise bound to the provided Thenable.

    Declaration

    Swift

    public init<U>(_ bridge: U) where T == U.T, U : Thenable
  • Initialize a new promise that can be resolved with the provided Resolver.

    Declaration

    Swift

    public init(resolver body: (Resolver<T>) throws -> Void)
  • Declaration

    Swift

    public class func pending() -> (promise: Promise<T>, resolver: Resolver<T>)

    Return Value

    a tuple of a new pending promise and its Resolver.

  • See

    Thenable.pipe

    Declaration

    Swift

    public func pipe(to: @escaping (Result<T>) -> Void)
  • Declaration

    Swift

    public var result: Result<T>? { get }
  • Undocumented

    Declaration

    Swift

    func future() -> Future<T, Error>
  • Declaration

    Swift

    public var description: String { get }

    Return Value

    A description of the state of this promise.

  • Declaration

    Swift

    public var debugDescription: String { get }

    Return Value

    A debug-friendly description of the state of this promise.

  • Undocumented

    Declaration

    Swift

    func always(on q: DispatchQueue = .main, execute body: @escaping () -> Void) -> Promise
  • Blocks this thread, so—you know—don’t call this on a serial thread that any part of your chain may use. Like the main thread for example.

    Declaration

    Swift

    func wait() throws -> T

Available where T == Any?

  • Undocumented

    Declaration

    Swift

    convenience init(_ anyPromise: AnyPromise)

Available where T == Void

  • Initializes a new promise fulfilled with Void

    Declaration

    Swift

    public convenience init()
  • Returns a new promise fulfilled with Void

    Declaration

    Swift

    public static var value: Promise<Void> { get }