API Reference
This page provides a summary of the core classes, functions, and operators available in Then
.
Promise<T>
The central class of the library, representing a value that may be available in the future.
Initializers
init()
: Creates a new, dormant promise.init(_ value: T)
: Creates a new, fulfilled promise with a value.init(error: Error)
: Creates a new, rejected promise with an error.init(callback: @escaping (resolve, reject) -> Void)
: Creates a promise that executes the given block. ForPromise<Void>
,resolve
takes no arguments.init(callback: @escaping (resolve, reject, progress) -> Void)
: An initializer that also provides aprogress
reporting closure.
Core Methods
start()
: Manually starts a dormant promise.then<X>(_ block: @escaping (T) -> X) -> Promise<X>
: Chains a synchronous operation.then<X>(_ block: @escaping (T) -> Promise<X>) -> Promise<X>
: Chains an asynchronous operation.onError(_ block: @escaping (Error) -> Void) -> Promise<Void>
: Handles any error in the chain.finally(_ block: @escaping () -> Void)
: Executes a block upon completion, regardless of success or failure.progress(_ block: @escaping (Float) -> Void) -> Promise<T>
: Listens for progress updates.
Helper Methods
recover(with value: T) -> Promise<T>
: Recovers from an error with a fallback value.recover(_ block: @escaping (Error) throws -> T) -> Promise<T>
: Recovers from an error using a block to provide a value.recover(_ block: @escaping (Error) throws -> Promise<T>) -> Promise<T>
: Recovers from an error by chaining a new promise.retry(_ nbOfTimes: UInt) -> Promise<T>
: Retries the promise on failure.validate(withError: Error, _ assertionBlock: @escaping (T) -> Bool) -> Promise<T>
: Rejects the promise if the assertion fails.delay(_ time: TimeInterval) -> Promise<T>
: Delays the continuation of the chain.timeout(_ time: TimeInterval) -> Promise<T>
: Rejects the promise if it doesn't resolve in time.chain(_ block: @escaping (T) -> Void) -> Promise<T>
: Performs a side-effect.noMatterWhat(_ block: @escaping () -> Void) -> Promise<T>
: Executes a block mid-chain regardless of outcome.bridgeError(to myError: Error) -> Promise<T>
: Maps any error to a custom error.
Promises
(Static Methods)
A namespace for functions that operate on multiple promises.
whenAll<T>(_ promises: [Promise<T>]) -> Promise<[T]>
: Fulfills when all promises fulfill.race<T>(_ promises: Promise<T>...) -> Promise<T>
: Fulfills or rejects with the first promise to complete.zip<T, U>(_ p1: Promise<T>, _ p2: Promise<U>) -> Promise<(T, U)>
: Combines results of different types. Overloads exist for up to 8 promises.delay(_ time: TimeInterval) -> Promise<Void>
: Returns a promise that resolves after a delay.
Global Functions
async<T>(block: @escaping () throws -> T) -> Async<T>
: Wraps a block in a background promise to enableawait
.awaitPromise<T>(_ promise: Promise<T>) throws -> T
: Synchronously waits for a promise to complete.unwrap<T>(_ param: T?) -> Promise<T>
: Converts an optional to a promise.
Operators
prefix func .. <T>(promise: Promise<T>) throws -> T
: Shorthand fortry awaitPromise(promise)
.prefix func ..? <T>(promise: Promise<T>) -> T?
: Shorthand for a failableawaitPromise
that returnsnil
on error instead of throwing.
Type Aliases
Async<T>
: An alias forPromise<T>
.AsyncTask
: An alias forPromise<Void>
.EmptyPromise
: An alias forPromise<Void>
.
PromiseError
A standard error enum used by Then
.
case .default
: A generic error.case .validationFailed
: Thrown by.validate()
.case .retryInvalidInput
: Thrown by.retry()
if input is invalid.case .unwrappingFailed
: Thrown byunwrap()
.case .timeout
: Thrown by.timeout()
.