Thenable
public protocol Thenable : AnyObject
Thenable represents an asynchronous operation that can be chained.
-
The type of the wrapped value
Declaration
Swift
associatedtype T -
pipeis immediately executed when thisThenableis resolvedDeclaration
Swift
func pipe(to: @escaping(Result<T, Error>) -> Void) -
The resolved result or nil if pending.
Declaration
Swift
var result: Result<T, Error>? { get }
-
then(on:flags:_:)Extension methodThe provided closure executes when this promise resolves.
This allows chaining promises. The promise returned by the provided closure is resolved before the promise returned by this closure resolves.
Declaration
Parameters
onThe queue to which the provided closure dispatches.
bodyThe closure that executes when this promise fulfills. 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 methodThe provided closure is executed when this promise is resolved.
This is like
thenbut it requires the closure to return a non-promise.Declaration
Swift
func map<U>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T) throws -> U) -> Promise<U>Parameters
onThe queue to which the provided closure dispatches.
transformThe closure that is executed when this Promise is fulfilled. It must return a non-promise.
Return Value
A new promise that is resolved with the value returned from the provided closure. For example:
firstly { URLSession.shared.dataTask(.promise, with: url1) }.map { response in response.data.length }.done { length in //… }
-
compactMap(on:flags:_:)Extension methodThe provided closure is executed when this promise is resolved.
In your closure return an
Optional, if you returnnilthe resulting promise is rejected withPMKError.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? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T) throws -> U?) -> Promise<U> -
done(on:flags:_:)Extension methodThe provided closure is executed when this promise is resolved.
Equivalent to
map { x -> Void in, but since we force theVoidreturn Swift is happier and gives you less hassle about your closure’s qualification.Declaration
Swift
func done(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(T) throws -> Void) -> Promise<Void>Parameters
onThe queue to which the provided closure dispatches.
bodyThe closure that is executed when this Promise is fulfilled.
Return Value
A new promise fulfilled as
Void.firstly { URLSession.shared.dataTask(.promise, with: url) }.done { response in print(response.data) }
-
get(on:flags:_:)Extension methodThe provided closure is executed when this promise is resolved.
This is like
donebut it returns the same value that the handler is fed.getimmutably accesses the fulfilled value; the returned Promise maintains that value.Declaration
Parameters
onThe queue to which the provided closure dispatches.
bodyThe closure that is executed when this Promise is fulfilled.
Return Value
A new promise that is resolved with the value that the handler is fed. 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 methodThe provided closure is executed with promise result.
This is like
getbut provides the Resultof 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
Parameters
onThe queue to which the provided closure dispatches.
bodyThe 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:
-
mapValues(on:flags:_:)Extension methodPromise<[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? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T.Iterator.Element) throws -> U) -> Promise<[U]> -
flatMapValues(on:flags:_:)Extension methodPromise<[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: Sequence>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T.Iterator.Element) throws -> U) -> Promise<[U.Iterator.Element]> -
compactMapValues(on:flags:_:)Extension methodPromise<[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? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T.Iterator.Element) throws -> U?) -> Promise<[U]> -
thenMap(on:flags:_:)Extension methodPromise<[T]>=>T->Promise<U>=>Promise<[U]>firstly { .value([1,2,3]) }.thenMap { integer in .value(integer * 2) }.done { // $0 => [2,4,6] } -
thenFlatMap(on:flags:_:)Extension methodPromise<[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] } -
filterValues(on:flags:_:)Extension methodPromise<[T]>=>T-> Bool =>Promise<[U]>firstly { .value([1,2,3]) }.filterValues { $0 > 1 }.done { // $0 => [2,3] }
-
firstValue(on:flags:where:)Extension method
-
sortedValues(on:flags:)Extension method
-
then(on:_:)Extension methodThe provided closure executes when this promise resolves.
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: Thenable>(on: Dispatcher = conf.D.map, _ body: @escaping(T) throws -> U) -> Promise<U.T>Parameters
onThe dispatcher that executes the provided closure.
bodyThe closure that executes when this promise fulfills. 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:_:)Extension methodThe provided closure is executed when this promise is resolved.
This is like
thenbut it requires the closure to return a non-promise.Declaration
Swift
func map<U>(on: Dispatcher = conf.D.map, _ transform: @escaping(T) throws -> U) -> Promise<U>Parameters
onThe dispatcher that executes the provided closure.
transformThe closure that is executed when this Promise is fulfilled. It must return a non-promise.
Return Value
A new promise that is resolved with the value returned from the provided closure. For example:
firstly { URLSession.shared.dataTask(.promise, with: url1) }.map { response in response.data.length }.done { length in //… }
-
compactMap(on:_:)Extension methodThe provided closure is executed when this promise is resolved.
In your closure return an
Optional, if you returnnilthe resulting promise is rejected withPMKError.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: Dispatcher = conf.D.map, _ transform: @escaping(T) throws -> U?) -> Promise<U> -
done(on:_:)Extension methodThe provided closure is executed when this promise is resolved.
Equivalent to
map { x -> Void in, but since we force theVoidreturn Swift is happier and gives you less hassle about your closure’s qualification.Declaration
Swift
func done(on: Dispatcher = conf.D.return, _ body: @escaping(T) throws -> Void) -> Promise<Void>Parameters
onThe dispatcher that executes the provided closure.
bodyThe closure that is executed when this Promise is fulfilled.
Return Value
A new promise fulfilled as
Void.firstly { URLSession.shared.dataTask(.promise, with: url) }.done { response in print(response.data) }
-
get(on:_:)Extension methodThe provided closure is executed when this promise is resolved.
This is like
donebut it returns the same value that the handler is fed.getimmutably accesses the fulfilled value; the returned Promise maintains that value.Declaration
Swift
func get(on: Dispatcher = conf.D.return, _ body: @escaping (T) throws -> Void) -> Promise<T>Parameters
onThe dispatcher that executes the provided closure.
bodyThe closure that is executed when this Promise is fulfilled.
Return Value
A new promise that is resolved with the value that the handler is fed. 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:_:)Extension methodThe provided closure is executed with promise result.
This is like
getbut provides the Resultof 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: Dispatcher = conf.D.map, _ body: @escaping(Result<T, Error>) -> Void) -> Promise<T>Parameters
onThe dispatcher that executes the provided closure.
bodyThe 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 methodDeclaration
Swift
func asVoid() -> Promise<Void>Return Value
a new promise chained off this promise but with its value discarded.
-
errorExtension methodDeclaration
Swift
var error: Error? { get }Return Value
The error with which this promise was rejected;
nilif this promise is not rejected. -
isPendingExtension methodDeclaration
Swift
var isPending: Bool { get }Return Value
trueif the promise has not yet resolved. -
isResolvedExtension methodDeclaration
Swift
var isResolved: Bool { get }Return Value
trueif the promise has resolved. -
isFulfilledExtension methodDeclaration
Swift
var isFulfilled: Bool { get }Return Value
trueif the promise was fulfilled. -
isRejectedExtension methodDeclaration
Swift
var isRejected: Bool { get }Return Value
trueif the promise was rejected. -
valueExtension methodDeclaration
Swift
var value: T? { get }Return Value
The value with which this promise was fulfilled or
nilif this promise is pending or rejected.
-
mapValues(on:_:)Extension methodPromise<[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: Dispatcher = conf.D.map, _ transform: @escaping(T.Iterator.Element) throws -> U) -> Promise<[U]> -
flatMapValues(on:_:)Extension methodPromise<[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: Sequence>(on: Dispatcher = conf.D.map, _ transform: @escaping(T.Iterator.Element) throws -> U) -> Promise<[U.Iterator.Element]> -
compactMapValues(on:_:)Extension methodPromise<[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: Dispatcher = conf.D.map, _ transform: @escaping(T.Iterator.Element) throws -> U?) -> Promise<[U]> -
thenMap(on:_:)Extension methodPromise<[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: Thenable>(on: Dispatcher = conf.D.map, _ transform: @escaping(T.Iterator.Element) throws -> U) -> Promise<[U.T]> -
thenFlatMap(on:_:)Extension methodPromise<[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: Thenable>(on: Dispatcher = conf.D.map, _ transform: @escaping(T.Iterator.Element) throws -> U) -> Promise<[U.T.Iterator.Element]> where U.T: Sequence -
filterValues(on:_:)Extension methodPromise<[T]>=>T-> Bool =>Promise<[U]>firstly { .value([1,2,3]) }.filterValues { $0 > 1 }.done { // $0 => [2,3] }Declaration
Swift
func filterValues(on: Dispatcher = conf.D.map, _ isIncluded: @escaping (T.Iterator.Element) -> Bool) -> Promise<[T.Iterator.Element]>
-
firstValueExtension method -
firstValue(on:where:)Extension methodUndocumented
Declaration
Swift
func firstValue(on: Dispatcher = conf.D.map, where test: @escaping (T.Iterator.Element) -> Bool) -> Promise<T.Iterator.Element> -
lastValueExtension method
-
sortedValues(on:)Extension methodDeclaration
Swift
func sortedValues(on: Dispatcher = conf.D.map) -> Promise<[T.Iterator.Element]>Return Value
a promise fulfilled with the sorted values of this
Sequence.
View on GitHub
Thenable Protocol Reference