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) -> PromiseParameters
makePromiseblock 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
dispatchblock 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
valuethe 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) -> VoidParameters
valuethe value to fulfill
Promises 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
errorthe 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) -> VoidParameters
errorthe error rejecting this promise.
-
Register a completion to be called when this promise is fulfilled. If this promise has already been fulfilled,
completionwill still be called. If this promise is rejected,completionwill not be called.Declaration
Swift
public func then(_ fulfillment: @autoclosure @escaping () -> (Value) -> Void) -> PromiseParameters
completionthe 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,
completionwill still be called. If this promise is rejected,completionwill 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) -> PromiseParameters
completionthe 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,
completionwill still be called. If this promise is rejected,completionwill not be called.The value returned by
completionwill 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
completionthe 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,
completionwill still be called. If this promise is rejected,completionwill not be called.The promise returned by
completionwill be used to fulfill or reject the returned promise.Declaration
Swift
public func then<NewValue>(_ fulfillment: @autoclosure @escaping () -> (Value) -> NewValue) -> Promise<NewValue>Parameters
completionthe 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,
completionwill still be called. If this promise is fulfilled,completionwill not be called.Declaration
Swift
public func `catch`(_ rejection: @autoclosure @escaping () -> (Error) -> Void) -> PromiseParameters
completionthe 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,
completionwill still be called.Declaration
Swift
public func finally(_ completion: @autoclosure @escaping () -> () -> Void) -> PromiseParameters
completionthe 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) -> PromiseParameters
queuea 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) -> PromiseParameters
queuea 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
promisewhich runs any completions on the main thread.Declaration
Swift
public static func main(_ promise: Promise) -> PromiseReturn 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() -> PromiseReturn 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,
completionwill still be called. If this promise is rejected,completionwill not be called.Declaration
Swift
public static func ?>(promise: Promise, f: @autoclosure @escaping () -> (Value) -> Void) -> PromiseParameters
promisethe promise on which to add the fulfillment.
completionthe 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,
completionwill still be called. If this promise is rejected,completionwill 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) -> PromiseParameters
promisethe promise on which to add the fulfillment.
completionthe 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,
completionwill still be called. If this promise is rejected,completionwill not be called.The promise returned by
See morecompletionwill be used to fulfill or reject the returned promise.Declaration
Parameters
promisethe promise on which to add the fulfillment.
completionthe 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,
completionwill still be called. If this promise is rejected,completionwill not be called.The value returned by
See morecompletionwill fulfill the returned promise, unless an error is thrown, which will reject the promise instead.Declaration
Parameters
promisethe promise on which to add the fulfillment.
completionthe 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,
completionwill still be called. If this promise is fulfilled,completionwill not be called.Declaration
Swift
public static func !>(promise: Promise, f: @autoclosure @escaping () -> (Error) -> Void) -> PromiseParameters
promisethe promise on which to add the completion.
completionthe 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,
completionwill still be called.Declaration
Swift
public static func *>(promise: Promise, f: @autoclosure @escaping () -> () -> Void) -> PromiseParameters
promisethe promise on which to add the completion.
completionthe completion to be called upon fulfillment or rejection.
Return Value
promise.
View on GitHub
Install in Dash
Promise Class Reference