Thenable

public protocol Thenable : AnyObject

Thenable represents an asynchronous operation that can be chained.

  • T

    The type of the wrapped value

    Declaration

    Swift

    associatedtype T
  • pipe is immediately executed when this Thenable is resolved

    Declaration

    Swift

    func pipe(to: @escaping (Result<T>) -> Void)
  • The resolved result or nil if pending.

    Declaration

    Swift

    var result: Result<T>? { get }
  • then(on:flags:_:) Extension method

    The provided closure executes when this promise is fulfilled.

    This allows chaining promises. The promise returned by the provided closure is resolved before the promise returned by this closure resolves.

    Declaration

    Swift

    func then<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (T) throws -> U) -> Promise<U.T> where U : Thenable

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that executes when this promise is fulfilled. It must return a promise.

    Return Value

    A new promise that resolves when the promise returned from the provided closure resolves. For example:

    firstly { URLSession.shared.dataTask(.promise, with: url1) }.then { response in transform(data: response.data) }.done { transformation in //… }

  • map(on:flags:_:) Extension method

    The provided closure is executed when this promise is fulfilled.

    This is like then but it requires the closure to return a non-promise.

    Declaration

    Swift

    func map<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (T) throws -> U) -> Promise<U>

    Parameters

    on

    The queue to which the provided closure dispatches.

    transform

    The closure that is executed when this Promise is fulfilled. It must return a non-promise.

    Return Value

    A new promise that is fulfilled with the value returned from the provided closure or rejected if the provided closure throws. For example:

    firstly { URLSession.shared.dataTask(.promise, with: url1) }.map { response in response.data.length }.done { length in //… }

  • map(on:flags:_:) Extension method

    Similar to func map<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T) throws -> U) -> Promise<U>, but accepts a key path instead of a closure.

    Declaration

    Swift

    func map<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ keyPath: KeyPath<T, U>) -> Promise<U>

    Parameters

    on

    The queue to which the provided key path for value dispatches.

    keyPath

    The key path to the value that is using when this Promise is fulfilled.

    Return Value

    A new promise that is fulfilled with the value for the provided key path.

  • compactMap(on:flags:_:) Extension method

    The provided closure is executed when this promise is fulfilled.

    In your closure return an Optional, if you return nil the resulting promise is rejected with PMKError.compactMap, otherwise the promise is fulfilled with the unwrapped value.

     firstly {
         URLSession.shared.dataTask(.promise, with: url)
     }.compactMap {
         try JSONSerialization.jsonObject(with: $0.data) as? [String: String]
     }.done { dictionary in
         //…
     }.catch {
         // either `PMKError.compactMap` or a `JSONError`
     }
    

    Declaration

    Swift

    func compactMap<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (T) throws -> U?) -> Promise<U>
  • compactMap(on:flags:_:) Extension method

    Similar to func compactMap<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T) throws -> U?) -> Promise<U>, but accepts a key path instead of a closure.

    Declaration

    Swift

    func compactMap<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ keyPath: KeyPath<T, U?>) -> Promise<U>

    Parameters

    on

    The queue to which the provided key path for value dispatches.

    keyPath

    The key path to the value that is using when this Promise is fulfilled. If the value for keyPath is nil the resulting promise is rejected with PMKError.compactMap.

    Return Value

    A new promise that is fulfilled with the value for the provided key path.

  • done(on:flags:_:) Extension method

    The provided closure is executed when this promise is fulfilled.

    Equivalent to map { x -> Void in, but since we force the Void return Swift is happier and gives you less hassle about your closure’s qualification.

    Declaration

    Swift

    func done(on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (T) throws -> Void) -> Promise<Void>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that is executed when this Promise is fulfilled.

    Return Value

    A new promise fulfilled as Void or rejected if the provided closure throws.

    firstly { URLSession.shared.dataTask(.promise, with: url) }.done { response in print(response.data) }

  • get(on:flags:_:) Extension method

    The provided closure is executed when this promise is fulfilled.

    This is like done but it returns the same value that the handler is fed. get immutably accesses the fulfilled value; the returned Promise maintains that value.

    Declaration

    Swift

    func get(on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (T) throws -> Void) -> Promise<T>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that is executed when this Promise is fulfilled.

    Return Value

    A new promise that is fulfilled with the value that the handler is fed or rejected if the provided closure throws. For example:

    firstly { .value(1) }.get { foo in print(foo, “ is 1”) }.done { foo in print(foo, “ is 1”) }.done { foo in print(foo, “ is Void”) }

  • tap(on:flags:_:) Extension method

    The provided closure is executed with promise result.

    This is like get but provides the Result of the Promise so you can inspect the value of the chain at this point without causing any side effects.

    promise.tap{ print($0) }.then{ /*…

    Declaration

    Swift

    func tap(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (Result<T>) -> Void) -> Promise<T>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that is executed with Result of Promise.

    Return Value

    A new promise that is resolved with the result that the handler is fed. For example:

  • asVoid() Extension method

    Declaration

    Swift

    func asVoid() -> Promise<Void>

    Return Value

    a new promise chained off this promise but with its value discarded.

  • error Extension method

    Declaration

    Swift

    var error: Error? { get }

    Return Value

    The error with which this promise was rejected; nil if this promise is not rejected.

  • isPending Extension method

    Declaration

    Swift

    var isPending: Bool { get }

    Return Value

    true if the promise has not yet resolved.

  • isResolved Extension method

    Declaration

    Swift

    var isResolved: Bool { get }

    Return Value

    true if the promise has resolved.

  • isFulfilled Extension method

    Declaration

    Swift

    var isFulfilled: Bool { get }

    Return Value

    true if the promise was fulfilled.

  • isRejected Extension method

    Declaration

    Swift

    var isRejected: Bool { get }

    Return Value

    true if the promise was rejected.

  • value Extension method

    Declaration

    Swift

    var value: T? { get }

    Return Value

    The value with which this promise was fulfilled or nil if this promise is pending or rejected.

Available where T: Sequence

  • filter(on:test:) Extension method

    Undocumented

    Declaration

    Swift

    func filter(on: DispatchQueue? = conf.Q.map, test: @escaping (T.Iterator.Element) -> Bool) -> Promise<[T.Iterator.Element]>

Available where T: Collection

  • first Extension method

    Undocumented

    Declaration

    Swift

    var first: Promise<T.Iterator.Element> { get }
  • last Extension method

    Undocumented

    Declaration

    Swift

    var last: Promise<T.Iterator.Element> { get }

Available where T: Sequence, T.Iterator.Element: Comparable

  • sorted(on:) Extension method

    Undocumented

    Declaration

    Swift

    func sorted(on: DispatchQueue? = conf.Q.map) -> Promise<[T.Iterator.Element]>

Available where T: Sequence

  • mapValues(on:flags:_:) Extension method

    Promise<[T]> => T -> U => Promise<[U]>

    firstly {
        .value([1,2,3])
    }.mapValues { integer in
        integer * 2
    }.done {
        // $0 => [2,4,6]
    }
    

    Declaration

    Swift

    func mapValues<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (T.Iterator.Element) throws -> U) -> Promise<[U]>
  • mapValues(on:flags:_:) Extension method

    Promise<[T]> => KeyPath<T, U> => Promise<[U]>

    firstly {
        .value([Person(name: "Max"), Person(name: "Roman"), Person(name: "John")])
    }.mapValues(\.name).done {
        // $0 => ["Max", "Roman", "John"]
    }
    

    Declaration

    Swift

    func mapValues<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ keyPath: KeyPath<T.Iterator.Element, U>) -> Promise<[U]>
  • flatMapValues(on:flags:_:) Extension method

    Promise<[T]> => T -> [U] => Promise<[U]>

    firstly {
        .value([1,2,3])
    }.flatMapValues { integer in
        [integer, integer]
    }.done {
        // $0 => [1,1,2,2,3,3]
    }
    

    Declaration

    Swift

    func flatMapValues<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (T.Iterator.Element) throws -> U) -> Promise<[U.Iterator.Element]> where U : Sequence
  • Promise<[T]> => T -> U? => Promise<[U]>

    firstly {
        .value(["1","2","a","3"])
    }.compactMapValues {
        Int($0)
    }.done {
        // $0 => [1,2,3]
    }
    

    Declaration

    Swift

    func compactMapValues<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (T.Iterator.Element) throws -> U?) -> Promise<[U]>
  • Promise<[T]> => KeyPath<T, U?> => Promise<[U]>

    firstly {
        .value([Person(name: "Max"), Person(name: "Roman", age: 26), Person(name: "John", age: 23)])
    }.compactMapValues(\.age).done {
        // $0 => [26, 23]
    }
    

    Declaration

    Swift

    func compactMapValues<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ keyPath: KeyPath<T.Iterator.Element, U?>) -> Promise<[U]>
  • thenMap(on:flags:_:) Extension method

    Promise<[T]> => T -> Promise<U> => Promise<[U]>

    firstly {
        .value([1,2,3])
    }.thenMap { integer in
        .value(integer * 2)
    }.done {
        // $0 => [2,4,6]
    }
    

    Declaration

    Swift

    func thenMap<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (T.Iterator.Element) throws -> U) -> Promise<[U.T]> where U : Thenable
  • thenFlatMap(on:flags:_:) Extension method

    Promise<[T]> => T -> Promise<[U]> => Promise<[U]>

    firstly {
        .value([1,2,3])
    }.thenFlatMap { integer in
        .value([integer, integer])
    }.done {
        // $0 => [1,1,2,2,3,3]
    }
    

    Declaration

    Swift

    func thenFlatMap<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (T.Iterator.Element) throws -> U) -> Promise<[U.T.Iterator.Element]> where U : Thenable, U.T : Sequence
  • filterValues(on:flags:_:) Extension method

    Promise<[T]> => T -> Bool => Promise<[T]>

    firstly {
        .value([1,2,3])
    }.filterValues {
        $0 > 1
    }.done {
        // $0 => [2,3]
    }
    

    Declaration

    Swift

    func filterValues(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ isIncluded: @escaping (T.Iterator.Element) -> Bool) -> Promise<[T.Iterator.Element]>
  • filterValues(on:flags:_:) Extension method

    Promise<[T]> => KeyPath<T, Bool> => Promise<[T]>

    firstly {
        .value([Person(name: "Max"), Person(name: "Roman", age: 26, isStudent: false), Person(name: "John", age: 23, isStudent: true)])
    }.filterValues(\.isStudent).done {
        // $0 => [Person(name: "John", age: 23, isStudent: true)]
    }
    

    Declaration

    Swift

    func filterValues(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ keyPath: KeyPath<T.Iterator.Element, Bool>) -> Promise<[T.Iterator.Element]>

Available where T: Collection

  • firstValue Extension method

    Declaration

    Swift

    var firstValue: Promise<T.Iterator.Element> { get }

    Return Value

    a promise fulfilled with the first value of this Collection or, if empty, a promise rejected with PMKError.emptySequence.

  • firstValue(on:flags:where:) Extension method

    Undocumented

    Declaration

    Swift

    func firstValue(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, where test: @escaping (T.Iterator.Element) -> Bool) -> Promise<T.Iterator.Element>
  • lastValue Extension method

    Declaration

    Swift

    var lastValue: Promise<T.Iterator.Element> { get }

    Return Value

    a promise fulfilled with the last value of this Collection or, if empty, a promise rejected with PMKError.emptySequence.

Available where T: Sequence, T.Iterator.Element: Comparable

  • sortedValues(on:flags:) Extension method

    Declaration

    Swift

    func sortedValues(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil) -> Promise<[T.Iterator.Element]>

    Return Value

    a promise fulfilled with the sorted values of this Sequence.