Promise

@interface Promise : NSObject

A Promise implementation that simplifies asynchronous code.

  • The block to be asynchronously executed that will resolve or reject the promise.

    Declaration

    Objective-C

    @property (readwrite, strong, nonatomic) Executor _Nonnull executor;

    Swift

    var executor: Executor { get set }
  • The Resolved blocks to be called when the promise is resolved.

    Declaration

    Objective-C

    @property (readwrite, strong, nonatomic)
        NSMutableArray *_Nonnull resultObservers;

    Swift

    var resultObservers: NSMutableArray { get set }
  • The Rejected blocks to be called when the promise is rejected.

    Declaration

    Objective-C

    @property (readwrite, strong, nonatomic) NSMutableArray *_Nonnull errorObservers;

    Swift

    var errorObservers: NSMutableArray { get set }
  • If the promise has been resolved.

    Declaration

    Objective-C

    @property (assign, readwrite, atomic) BOOL resolved;

    Swift

    var resolved: Bool { get set }
  • The result of the promise in case it has been resolved.

    Declaration

    Objective-C

    @property (readwrite, strong, nonatomic) id _Nullable result;

    Swift

    var result: Any? { get set }
  • The error of the promise in case it has been rejected.

    Declaration

    Objective-C

    @property (readwrite, strong, nonatomic) NSError *_Nonnull error;

    Swift

    var error: Error { get set }
  • The constructor of a Promise instance.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithExecutor:(nonnull Executor)executor;

    Swift

    init(executor: @escaping Executor)

    Parameters

    executor

    The block to be asynchronously executed that will resolve or reject the promise.

    Return Value

    The Promise instance created.

  • Calls the provided Resolved block with the result of the promise if it is resolved.

    Declaration

    Objective-C

    - (nonnull Promise *)then:(nonnull Resolved)resolved;

    Swift

    func then(_ resolved: @escaping Resolved) -> Promise

    Parameters

    resolved

    The Resolved block to be called.

    Return Value

    The Promise instance itself.

  • Calls the provided Rejected block with an error caused by a rejection of the promise.

    Declaration

    Objective-C

    - (void) catch:(nonnull Rejected)rejected;

    Swift

    func `catch`(_ rejected: @escaping Rejected)

    Parameters

    rejected

    The Rejected block to be called.

  • Helper method that executes multiple promises in parallel and returns a Promise that is resolved when all the provided promises have finished executing.

    Declaration

    Objective-C

    + (nonnull Promise *)all:(nonnull NSArray *)promises;

    Swift

    class func all(_ promises: [Any]) -> Promise

    Parameters

    promises

    The promises to be executed.

    Return Value

    A Promise instance where the result is a NSArray with the results of all the provided promises.