Promise

public class Promise : @unchecked Sendable

约定类

Accessor

  • 约定失败错误,约定失败时默认使用,可用于错误判断,支持自定义

    Declaration

    Swift

    public static var failedError: Error { get set }
  • 约定验证错误,验证失败时默认使用,可用于错误判断,支持自定义

    Declaration

    Swift

    public static var validationError: Error { get set }
  • 约定超时错误,约定超时时默认使用,可用于错误判断,支持自定义

    Declaration

    Swift

    public static var timeoutError: Error { get set }

Lifecycle

  • 指定操作完成句柄初始化

    Declaration

    Swift

    public init(completion: @escaping @MainActor @Sendable (_ completion: @escaping @MainActor @Sendable (_ result: Sendable) -> Void) -> Void)
  • 指定操作成功和失败句柄初始化

    Declaration

    Swift

    public convenience init<T>(block: @escaping @MainActor @Sendable (_ resolve: @escaping @MainActor @Sendable (_ value: T) -> Void, _ reject: @escaping @MainActor @Sendable (_ error: Error) -> Void) -> Void) where T : Sendable
  • 指定操作成功、失败句柄和进度句柄初始化

    Declaration

    Swift

    public convenience init<T>(progress: @escaping @MainActor @Sendable (_ resolve: @escaping @MainActor @Sendable (_ value: T) -> Void, _ reject: @escaping @MainActor @Sendable (_ error: Error) -> Void, _ progress: @escaping @MainActor @Sendable (_ value: Double) -> Void) -> Void) where T : Sendable
  • 快速创建成功实例

    Declaration

    Swift

    public convenience init(value: Sendable)
  • 快速创建失败实例

    Declaration

    Swift

    public convenience init(error: Error)

Public

  • 全部约定,所有约定成功才返回约定结果合集;如果某一个失败了,则返回该错误信息;约定进度为所有约定总进度

    Declaration

    Swift

    public static func all(_ promises: [Promise]) -> Promise
  • 某个约定,返回最先成功的约定结果;如果都失败了,返回最后一个错误信息;约定进度为最先成功的约定进度

    Declaration

    Swift

    public static func any(_ promises: [Promise]) -> Promise
  • 约定竞速,返回最先结束的约定结果,不管成功或失败;约定进度为最先结束的约定进度

    Declaration

    Swift

    public static func race(_ promises: [Promise]) -> Promise
  • 延时约定,延时完成时必定成功

    Declaration

    Swift

    public static func delay(_ time: TimeInterval) -> Promise
  • 约定重试,block需返回新创建的约定,该约定失败时延迟指定时间后重新创建并执行,直至成功或达到最大重试次数(总次数retry+1)

    Declaration

    Swift

    public static func retry(_ times: Int = 1, delay: TimeInterval = 0, block: @escaping @MainActor @Sendable () -> Promise) -> Promise
  • 执行约定并回调完成句柄

    Declaration

    Swift

    public func done(_ completion: @escaping @MainActor @Sendable (_ result: Sendable) -> Void)
  • 执行约定并分别回调成功、失败句柄,统一回调收尾句柄

    Declaration

    Swift

    public func done<T>(_ done: @escaping @MainActor @Sendable (_ value: T) -> Void, catch: (@MainActor @Sendable (_ error: Error) -> Void)?, finally: (@MainActor @Sendable () -> Void)? = nil) where T : Sendable
  • 执行约定并分别回调成功、失败句柄、进度句柄,统一回调收尾句柄

    Declaration

    Swift

    public func done<T>(_ done: @escaping @MainActor @Sendable (_ value: T) -> Void, catch: (@MainActor @Sendable (_ error: Error) -> Void)?, progress: (@MainActor @Sendable (_ value: Double) -> Void)?, finally: (@MainActor @Sendable () -> Void)? = nil) where T : Sendable
  • 执行当前约定,成功时调用句柄处理结果或者返回下一个约定

    Declaration

    Swift

    public func then<T>(_ block: @escaping @MainActor @Sendable (_ value: T) throws -> Sendable) -> Promise where T : Sendable
  • 执行当前约定,失败时调用句柄恢复结果或者返回下一个约定

    Declaration

    Swift

    public func recover(_ block: @escaping @MainActor @Sendable (_ error: Error) throws -> Sendable) -> Promise
  • 验证约定,当前约定成功时验证结果,可返回Bool或抛异常;验证通过时返回结果,验证失败时返回验证错误

    Declaration

    Swift

    public func validate<T>(_ block: @escaping @MainActor @Sendable (_ value: T) throws -> Bool) -> Promise where T : Sendable
  • 减少约定,当前约定结果作为初始值value,顺序使用value和数组值item调用reducer,产生新的value继续循环直至结束,类似数组reduce方法

    Declaration

    Swift

    public func reduce<T>(_ items: [T], reducer: @escaping @MainActor @Sendable (_ value: Sendable, _ item: T) throws -> Sendable) -> Promise where T : Sendable
  • 约定延时,当前约定成功时延时返回结果;默认失败时不延时,可设置force强制失败时也延时

    Declaration

    Swift

    public func delay(_ time: TimeInterval, force: Bool = false) -> Promise
  • 约定超时,当前约定未超时时返回结果;否则返回超时错误信息

    Declaration

    Swift

    public func timeout(_ time: TimeInterval, error: Error? = nil) -> Promise
  • 约定重试,block需返回新创建的约定,当前约定失败时延迟指定时间后调用block创建约定并执行,直至成功或达到最大重试次数

    Declaration

    Swift

    public func retry(_ times: Int = 1, delay: TimeInterval = 0, block: @escaping @MainActor @Sendable () -> Promise) -> Promise

Concurrency+Promise

  • value Asynchronous

    异步获取结果值

    Declaration

    Swift

    public var value: Sendable { get async throws }
  • value() Asynchronous

    异步获取结果值,可声明类型

    Declaration

    Swift

    public func value<T>() async throws -> T where T : Sendable