Promise
public final class Promise<Value>
A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled.
-
Wrap creating a promise in a promise.
Declaration
Swift
public class func `try`(_ makePromise: () throws -> Promise) -> Promise
Parameters
makePromise
block which creates a promise. Errors thrown will be reflected in the returned promise.
Return Value
the promise returned by
makePromise
, or rejected by an error thrown bymakePromise
. -
Initialize a new promise. Callbacks will be performed by calling
dispatch
.Declaration
Swift
public required init(dispatch: @escaping (@escaping () -> Void) -> Void = { $0() })
Parameters
dispatch
block used to execute callbacks.
-
Fulfills this promise with a value. Any previous or future calls to
then()
will be completed withvalue
. If this promise has already been fulfilled or rejected, this function does nothing.Declaration
Swift
public func fulfill(with value: Value)
Parameters
value
the value fulfilling this promise.
-
Returns a function which fulfills a promise with a value. Any previous or future calls to
promise.then()
will be fulfilled withvalue
. If the promise has already been fulfilled or rejected, the returned function does nothing.Declaration
Swift
public static func fulfill(with value: @autoclosure @escaping () -> Value) -> (Promise) -> Void
Parameters
value
the value to fulfill
Promise
s with. -
Rejects this promise with an error. Any previous or future calls to
catch()
will be completed witherror
. If this promise has already been fulfilled or rejected, this function does nothing.Declaration
Swift
public func reject(with error: Error)
Parameters
error
the error rejecting this promise.
-
Returns a function which rejects a promise with an error. Any previous or future calls to
promise.catch()
will be completed witherror
. If the promise has already been fulfilled or rejected, the returned function does nothing.Declaration
Swift
public static func reject(with error: @autoclosure @escaping () -> Error) -> (Promise) -> Void
Parameters
error
the error rejecting this promise.
-
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.Declaration
Swift
public func then(_ fulfillment: @autoclosure @escaping () -> (Value) -> Void) -> Promise
Parameters
completion
the completion to be called upon fulfillment.
Return Value
self
-
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.The value this promise was fulfilled with will fulfill the returned promise, unless an error is thrown by
completion
, which will reject the promise instead.Declaration
Swift
public func then(_ fulfillment: @autoclosure @escaping () -> (Value) throws -> Void) -> Promise
Parameters
completion
the completion to be called upon fulfillment.
Return Value
a new promise to be fulfilled or rejected by
completion
. -
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.The value returned by
completion
will fulfill the returned promise, unless an error is thrown, which will reject the promise instead.Declaration
Swift
public func then<NewValue>(_ fulfillment: @autoclosure @escaping () -> (Value) throws -> NewValue) -> Promise<NewValue>
Parameters
completion
the completion to be called upon fulfillment.
Return Value
a new promise to be fulfilled or rejected by
completion
. -
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.The promise returned by
completion
will be used to fulfill or reject the returned promise.Declaration
Swift
public func then<NewValue>(_ fulfillment: @autoclosure @escaping () -> (Value) -> NewValue) -> Promise<NewValue>
Parameters
completion
the completion to be called upon fulfillment.
Return Value
a new promise to be fulfilled or rejected by
completion
. -
Register a completion to be called when this promise is rejected. If this promise has already been rejected,
completion
will still be called. If this promise is fulfilled,completion
will not be called.Declaration
Swift
public func `catch`(_ rejection: @autoclosure @escaping () -> (Error) -> Void) -> Promise
Parameters
completion
the completion to be called upon rejection.
Return Value
self
-
Register a completion to be called when this promise is fulfilled or rejected. If this promise has already been fulfilled or rejected,
completion
will still be called.Declaration
Swift
public func finally(_ completion: @autoclosure @escaping () -> () -> Void) -> Promise
Parameters
completion
the completion to be called upon fulfillment or rejection.
Return Value
self
-
Create a new promise fulfilled or rejected by this promise which runs any completions on
queue
.Declaration
Swift
public static func async(with queue: DispatchQueue) -> (Promise) -> Promise
Parameters
queue
a dispatch queue on which to run completions.
Return Value
a block returning new promise to be fulfilled or rejected by the provided promise.
-
Create a new promise fulfilled or rejected by this promise which runs any completions on
queue
.Declaration
Swift
public func async(with queue: DispatchQueue) -> Promise
Parameters
queue
a dispatch queue on which to run completions.
Return Value
a new promise to be fulfilled or rejected by
self
. -
Create a new promise fulfilled or rejected by
promise
which runs any completions on the main thread.Declaration
Swift
public static func main(_ promise: Promise) -> Promise
Return Value
a new promise to be fulfilled or rejected by
promise
. -
Create a new promise fulfilled or rejected by this promise which runs any completions on the main thread.
Declaration
Swift
public func main() -> Promise
Return Value
a new promise to be fulfilled or rejected by
self
.
-
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.Declaration
Swift
public static func ?>(promise: Promise, f: @autoclosure @escaping () -> (Value) -> Void) -> Promise
Parameters
promise
the promise on which to add the fulfillment.
completion
the completion to be called upon fulfillment.
Return Value
promise
-
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.The value this promise was fulfilled with will fulfill the returned promise, unless an error is thrown by
completion
, which will reject the promise instead.Declaration
Swift
public static func ?>(promise: Promise, f: @autoclosure @escaping () -> (Value) throws -> Void) -> Promise
Parameters
promise
the promise on which to add the fulfillment.
completion
the completion to be called upon fulfillment.
Return Value
a new promise to be fulfilled or rejected by
completion
. -
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.The promise returned by
See morecompletion
will be used to fulfill or reject the returned promise.Declaration
Parameters
promise
the promise on which to add the fulfillment.
completion
the completion to be called upon fulfillment.
Return Value
a new promise to be fulfilled or rejected by
completion
. -
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completion
will still be called. If this promise is rejected,completion
will not be called.The value returned by
See morecompletion
will fulfill the returned promise, unless an error is thrown, which will reject the promise instead.Declaration
Parameters
promise
the promise on which to add the fulfillment.
completion
the completion to be called upon fulfillment.
Return Value
a new promise to be fulfilled or rejected by
completion
. -
Register a completion to be called when this promise is rejected. If this promise has already been rejected,
completion
will still be called. If this promise is fulfilled,completion
will not be called.Declaration
Swift
public static func !>(promise: Promise, f: @autoclosure @escaping () -> (Error) -> Void) -> Promise
Parameters
promise
the promise on which to add the completion.
completion
the completion to be called upon rejection.
Return Value
promise
. -
Register a completion to be called when this promise is fulfilled or rejected. If this promise has already been fulfilled or rejected,
completion
will still be called.Declaration
Swift
public static func *>(promise: Promise, f: @autoclosure @escaping () -> () -> Void) -> Promise
Parameters
promise
the promise on which to add the completion.
completion
the completion to be called upon fulfillment or rejection.
Return Value
promise
.