Other Functions

The following functions are available globally.

  • Undocumented

    Declaration

    Swift

    public func wrap<T>(_ body: (@escaping (T?, Error?) -> Void) throws -> Void) -> Promise<T>
  • Undocumented

    Declaration

    Swift

    public func wrap<T>(_ body: (@escaping (T, Error?) -> Void) throws -> Void) -> Promise<T>
  • Undocumented

    Declaration

    Swift

    public func wrap<T>(_ body: (@escaping (Error?, T?) -> Void) throws -> Void) -> Promise<T>
  • Undocumented

    Declaration

    Swift

    public func wrap(_ body: (@escaping (Error?) -> Void) throws -> Void) -> Promise<Void>
  • Undocumented

    Declaration

    Swift

    public func wrap<T>(_ body: (@escaping (T) -> Void) throws -> Void) -> Promise<T>
  •  after(seconds: 1.5).then {
         //…
     }
    

    Declaration

    Swift

    public func after(seconds: TimeInterval) -> Guarantee<Void>

    Return Value

    A guarantee that resolves after the specified duration.

  • after(.seconds(2)).then {
        //…
    }
    

    Declaration

    Swift

    public func after(_ interval: DispatchTimeInterval) -> Guarantee<Void>

    Return Value

    A guarantee that resolves after the specified duration.

  • Judicious use of firstly may make chains more readable.

    Compare:

    URLSession.shared.dataTask(url: url1).then {
        URLSession.shared.dataTask(url: url2)
    }.then {
        URLSession.shared.dataTask(url: url3)
    }
    

    With:

    firstly {
        URLSession.shared.dataTask(url: url1)
    }.then {
        URLSession.shared.dataTask(url: url2)
    }.then {
        URLSession.shared.dataTask(url: url3)
    }
    

    Note

    the block you pass executes immediately on the current thread/queue.

    Declaration

    Swift

    public func firstly<U>(execute body: () throws -> U) -> Promise<U.T> where U : Thenable
  • See

    firstly()

    Declaration

    Swift

    public func firstly<T>(execute body: () -> Guarantee<T>) -> Guarantee<T>
  • Runs the active run-loop until the provided promise resolves.

    This is for debug and is not a generally safe function to use in your applications. We mostly provide it for use in testing environments.

    Still if you like, study how it works (by reading the sources!) and use at your own risk.

    Throws

    An error, should the promise be rejected

    See

    wait()

    Declaration

    Swift

    public func hang<T>(_ promise: Promise<T>) throws -> T

    Return Value

    The value of the resolved promise

  • Waits for one promise to resolve

    race(promise1, promise2, promise3).then { winner in
        //…
    }
    

    Warning

    If the first resolution is a rejection, the returned promise is rejected

    Declaration

    Swift

    public func race<U>(_ thenables: U...) -> Promise<U.T> where U : Thenable

    Return Value

    The promise that resolves first

  • Waits for one promise to resolve

    race(promise1, promise2, promise3).then { winner in
        //…
    }
    

    Warning

    If the first resolution is a rejection, the returned promise is rejected

    Remark

    If the provided array is empty the returned promise is rejected with PMKError.badInput

    Declaration

    Swift

    public func race<U>(_ thenables: [U]) -> Promise<U.T> where U : Thenable

    Return Value

    The promise that resolves first

  • Waits for one guarantee to resolve

    race(promise1, promise2, promise3).then { winner in
        //…
    }
    

    Declaration

    Swift

    public func race<T>(_ guarantees: Guarantee<T>...) -> Guarantee<T>

    Return Value

    The guarantee that resolves first

  • Waits for one promise to fulfill

    race(fulfilled: [promise1, promise2, promise3]).then { winner in
        //…
    }
    

    Warning

    Skips all rejected promises.

    Remark

    If the provided array is empty, the returned promise is rejected with PMKError.badInput. If there are no fulfilled promises, the returned promise is rejected with PMKError.noWinner.

    Declaration

    Swift

    public func race<U>(fulfilled thenables: [U]) -> Promise<U.T> where U : Thenable

    Return Value

    The promise that was fulfilled first.

  • Wait for all promises in a set to fulfill.

    For example:

    when(fulfilled: promise1, promise2).then { results in
        //…
    }.catch { error in
        switch error {
        case URLError.notConnectedToInternet:
            //…
        case CLError.denied:
            //…
        }
    }
    

    Note

    If any of the provided promises reject, the returned promise is immediately rejected with that error.

    Warning

    In the event of rejection the other promises will continue to resolve and, as per any other promise, will either fulfill or reject. This is the right pattern for getter style asynchronous tasks, but often for setter tasks (eg. storing data on a server), you most likely will need to wait on all tasks and then act based on which have succeeded and which have failed, in such situations use when(resolved:).

    Note

    when provides NSProgress.

    See also

    when(resolved:)

    Declaration

    Swift

    public func when<U>(fulfilled thenables: [U]) -> Promise<[U.T]> where U : Thenable

    Parameters

    promises

    The promises upon which to wait before the returned promise resolves.

    Return Value

    A new promise that resolves when all the provided promises fulfill or one of the provided promises rejects.

  • Wait for all promises in a set to fulfill.

    Declaration

    Swift

    public func when<U>(fulfilled promises: U...) -> Promise<Void> where U : Thenable, U.T == ()
  • Wait for all promises in a set to fulfill.

    Declaration

    Swift

    public func when<U>(fulfilled promises: [U]) -> Promise<Void> where U : Thenable, U.T == ()
  • Wait for all promises in a set to fulfill.

    Declaration

    Swift

    public func when<U, V>(fulfilled pu: U, _ pv: V) -> Promise<(U.T, V.T)> where U : Thenable, V : Thenable
  • Wait for all promises in a set to fulfill.

    Declaration

    Swift

    public func when<U, V, W>(fulfilled pu: U, _ pv: V, _ pw: W) -> Promise<(U.T, V.T, W.T)> where U : Thenable, V : Thenable, W : Thenable
  • Wait for all promises in a set to fulfill.

    Declaration

    Swift

    public func when<U, V, W, X>(fulfilled pu: U, _ pv: V, _ pw: W, _ px: X) -> Promise<(U.T, V.T, W.T, X.T)> where U : Thenable, V : Thenable, W : Thenable, X : Thenable
  • Wait for all promises in a set to fulfill.

    Declaration

    Swift

    public func when<U, V, W, X, Y>(fulfilled pu: U, _ pv: V, _ pw: W, _ px: X, _ py: Y) -> Promise<(U.T, V.T, W.T, X.T, Y.T)> where U : Thenable, V : Thenable, W : Thenable, X : Thenable, Y : Thenable
  • Generate promises at a limited rate and wait for all to fulfill.

    For example:

    func downloadFile(url: URL) -> Promise<Data> {
        // ...
    }
    
    let urls: [URL] = /*…
    

    Declaration

    Swift

    public func when<It>(fulfilled promiseIterator: It, concurrently: Int) -> Promise<[It.Element.T]> where It : IteratorProtocol, It.Element : Thenable
  • Waits on all provided promises.

    when(fulfilled:) rejects as soon as one of the provided promises rejects. when(resolved:) waits on all provided promises whatever their result, and then provides an array of Result<T> so you can individually inspect the results. As a consequence this function returns a Guarantee, ie. errors are lifted from the individual promises into the results array of the returned Guarantee.

    when(resolved: promise1, promise2, promise3).then { results in
        for result in results where case .fulfilled(let value) {
           //…
        }
    }.catch { error in
        // invalid! Never rejects
    }
    

    Note

    we do not provide tuple variants for when(resolved:) but will accept a pull-request

    Remark

    Doesn’t take Thenable due to protocol associatedtype paradox

    Declaration

    Swift

    public func when<T>(resolved promises: Promise<T>...) -> Guarantee<[Result<T>]>

    Return Value

    A new promise that resolves once all the provided promises resolve. The array is ordered the same as the input, ie. the result order is not resolution order.

  • See

    when(resolved: Promise<T>...)

    Declaration

    Swift

    public func when<T>(resolved promises: [Promise<T>]) -> Guarantee<[Result<T>]>
  • Generate promises at a limited rate and wait for all to resolve.

    For example:

    func downloadFile(url: URL) -> Promise<Data> {
        // ...
    }
    
    let urls: [URL] = /*…
    
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when(_ guarantees: Guarantee<Void>...) -> Guarantee<Void>
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when<T>(_ guarantees: Guarantee<T>...) -> Guarantee<[T]>
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when(guarantees: [Guarantee<Void>]) -> Guarantee<Void>
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when<T>(guarantees: [Guarantee<T>]) -> Guarantee<[T]>
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when<U, V>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>) -> Guarantee<(U, V)>
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when<U, V, W>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>, _ gw: Guarantee<W>) -> Guarantee<(U, V, W)>
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when<U, V, W, X>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>, _ gw: Guarantee<W>, _ gx: Guarantee<X>) -> Guarantee<(U, V, W, X)>
  • Waits on all provided Guarantees.

    Declaration

    Swift

    public func when<U, V, W, X, Y>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>, _ gw: Guarantee<W>, _ gx: Guarantee<X>, _ gy: Guarantee<Y>) -> Guarantee<(U, V, W, X, Y)>