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. -
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 rejectedSee
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 rejectedReturn 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 rejectedRemark
If the provided array is empty the returned promise is rejected with PMKError.badInputReturn Value
The promise 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 withPMKError.badInput
. If there are no fulfilled promises, the returned promise is rejected withPMKError.noWinner
.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 forgetter
style asynchronous tasks, but often forsetter
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 usewhen(resolved:)
.Note
when
providesNSProgress
.See also
when(resolved:)
Declaration
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.
-
-
Generate promises at a limited rate and wait for all to fulfill.
For example:
func downloadFile(url: URL) -> Promise<Data> { // ... } let urls: [URL] = /*…
-
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 ofResult<T>
so you can individually inspect the results. As a consequence this function returns aGuarantee
, ie. errors are lifted from the individual promises into the results array of the returnedGuarantee
.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 forwhen(resolved:)
but will accept a pull-requestRemark
Doesn’t take Thenable due to protocolassociatedtype
paradoxReturn 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.
-
Generate promises at a limited rate and wait for all to resolve.
For example:
func downloadFile(url: URL) -> Promise<Data> { // ... } let urls: [URL] = /*…