PromiseLite
public class PromiseLite<Value>
An object that represents the eventual completion or failure of an asynchronous operation, and its resulting value.
-
Creates a promise and executes the given executor.
Declaration
Swift
public convenience init(_ executor: (_ resolve: @escaping (Value) -> Void, _ reject: @escaping (Error) -> Void) throws -> Void)
Parameters
executor
The function to be executed by the constructor, during the process of constructing the promise.
-
Creates a promise and executes the given executor.
Declaration
Swift
public convenience init(_ description: String, executor: (_ resolve: @escaping (Value) -> Void, _ reject: @escaping (Error) -> Void) throws -> Void)
Parameters
description
An optional custom description for the promise, eg.
fetchUserProfile
. Default description for a promise is"PromiseLite<\(Value.self)>"
, eg."PromiseLite<Bool>"
.executor
The function to be executed by the constructor, during the process of constructing the promise.
-
Returns a promise.
Declaration
Swift
@discardableResult public func flatMap<NewValue>(_ completion: @escaping (Value) throws -> PromiseLite<NewValue>) -> PromiseLite<NewValue>
Parameters
completion
A completion block that is called if the promise fulfilled.
-
Returns a Promise and deals with rejected cases only.
The Promise returned by
catch()
is rejected if the rejection block throws an error or returns a Promise which is itself rejected; otherwise, it is resolved.Declaration
Swift
@discardableResult public func flatCatch(_ rejection: @escaping (Error) throws -> PromiseLite) -> PromiseLite
Parameters
rejection
A completion block that is called when the Promise is rejected.
-
Returns a Promise and deals with rejected cases only.
The Promise returned by
catch()
is rejected if the rejection block throws an error or returns a Promise which is itself rejected; otherwise, it is resolved.Declaration
Swift
@discardableResult public func `catch`(_ rejection: @escaping (Error) throws -> Value) -> PromiseLite
Parameters
rejection
A completion block that is called when the Promise is rejected.
-
Returns a promise. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the Promise has been dealt with.
A finally callback will not receive any argument, since there’s no reliable means of determining if the promise was fulfilled or rejected. This use case is for precisely when you do not care about the rejection reason, or the fulfillment value, and so there’s no need to provide it.
Declaration
Swift
@discardableResult public func flatFinally<NewValue>(_ completion: @escaping () throws -> PromiseLite<NewValue>) -> PromiseLite<NewValue>
Parameters
completion
A completion block that is called if the promise is settled, whether fulfilled or rejected.
-
Returns a promise. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the Promise has been dealt with.
A finally callback will not receive any argument, since there’s no reliable means of determining if the promise was fulfilled or rejected. This use case is for precisely when you do not care about the rejection reason, or the fulfillment value, and so there’s no need to provide it.
Declaration
Swift
@discardableResult public func finally<NewValue>(_ completion: @escaping () throws -> NewValue) -> PromiseLite<NewValue>
Parameters
completion
A completion block that is called if the promise is settled, whether fulfilled or rejected.
-
Returns a promise.
Declaration
Swift
@discardableResult public func map<NewValue>(_ completion: @escaping (Value) throws -> NewValue) -> PromiseLite<NewValue>
Parameters
completion
A completion block that is called if the promise fulfilled.
-
Returns a Promise that is resolved with a given value.
Declaration
Swift
public static func resolve(_ value: Value) -> PromiseLite<Value>
Parameters
value
The argument to be resolved by this Promise.
-
Returns a Promise that is resolved with a given value.
Declaration
Swift
public static func resolve(description: String, _ value: Value) -> PromiseLite<Value>
Parameters
description
An optional custom description for the promise, eg.
fetchUserProfile
.value
The argument to be resolved by this Promise.
-
Returns a Promise that is rejected with a given reason.
Declaration
Swift
public static func reject(_ error: Error) -> PromiseLite<Value>
Parameters
error
The reason why this Promise rejected.
-
Returns a Promise that is rejected with a given reason.
Declaration
Swift
public static func reject(description: String, _ error: Error) -> PromiseLite<Value>
Parameters
description
An optional custom description for the promise, eg.
fetchUserProfile
.error
The reason why this Promise rejected.