An minimal Promise Library for Swift.
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/nfam/promise.swift.git", majorVersion: 0)
]
)
API is similar to the standard Promise in JavaScript.
Creates a Promise
with a closure that is passed two callback closure parameters to fulfill or reject the Promise
.
The body
initiates some asynchronous work, and then, once that completes,
either calls the first or second callback closure parameters to fulfill or
reject the promise, respectively.
If an error is thrown in the body
, the Promise
is rejected.
init<T>(
in queue: DispatchQueue? = nil,
execute body: ((T) -> Void, (Error) -> Void) -> Void
)
Creates a Promise
with a closure that returns a value to fulfill the Promise
.
The body
initiates some asynchronous work, and then, once that completes,
returns a value to fulfill the promise.
If an error is thrown in the body
, the Promise
is rejected.
init<T>(
in queue: DispatchQueue? = nil,
execute body: () throws -> T
)
Creates a Promise
that is fulfilled with a given value.
init<T>(in queue: DispatchQueue? = nil, value: T)
Creates a Promise
that is rejected with a given error.
init(in queue: DispatchQueue? = nil, error: Error)
Returns a Promise
that either fulfills when all of the promises
in the iterable parameter have resolved, or rejects as soon as one
of the promises in the iterable parameter rejects.
Promise.all<S>(resolved promises: S) -> Promise<[T]> where S: Sequence, S.Iterator.Element == Promise
Promise.all(_ promises: Promise<T>...) -> Promise<[T]>
Returns a Promise
that fulfills or rejects as soon as one of
the promises in the iterable fulfills or rejects, with the value or
reason from that Promise
.
Promise.race<S>(promises: S) -> Promise<T> where S: Sequence, S.Iterator.Element == Promise
Promise.race(_ promises: Promise<T>...) -> Promise<T>
Appends fulfillment and/or rejection closures to the Promise
, and returns a new Promise
resolving to the return value of the either executed closure.
func then<U>(
in queue: DispatchQueue? = nil,
fulfillment: (T) throws -> U
) -> Promise<U>
func then<U>(
in queue: DispatchQueue? = nil,
fulfillment: (T) throws -> Promise<U>
) -> Promise<U>
func then<U>(
in queue: DispatchQueue? = nil,
fulfillment: (T) throws -> U,
rejection: (Error) throws -> U
) -> Promise<U>
func then<U>(
in queue: DispatchQueue? = nil,
fulfillment: (T) throws -> Promise<U>,
rejection: (Error) throws -> Promise<U>
) -> Promise<U>
Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled. The Promise returned by catch() is rejected if onRejected throws an error or returns a Promise which is itself rejected; otherwise, it is resolved.
promise.catch { error in
...
}
Appends a handler to the promise, and returns a new promise which is resolved when the original promise is resolved. The handler is called when the promise is settled, whether fulfilled or rejected.
However, please note: a throw (or returning a rejected promise) in the finally callback will reject the new promise with that rejection reason.
promise.finally {
...
}