PromiseContext

public enum PromiseContext : Equatable, Hashable

The context in which a Promise body or callback is evaluated.

Most of these values correspond with Dispatch QoS classes.

  • Execute on the main queue.

    Note

    Chained callbacks on the .main context guarantee that they all execute within the same run loop pass. This means UI manipulations in chained callbacks on .main will all occur within the same CoreAnimation transaction. The only exception is if a callback returns an unresolved nested promise, as the subsequent callbacks must wait for that promise to resolve first.

    Declaration

    Swift

    case main
  • Execute on a dispatch queue with the .background QoS.

    Declaration

    Swift

    case background
  • Execute on a dispatch queue with the .utility QoS.

    Declaration

    Swift

    case utility
  • Execute on a dispatch queue with the .default QoS.

    Declaration

    Swift

    case `default`
  • Execute on a dispatch queue with the .userInitiated QoS.

    Declaration

    Swift

    case userInitiated
  • Execute on a dispatch queue with the .userInteractive QoS.

    Declaration

    Swift

    case userInteractive
  • Execute on the specified dispatch queue.

    Declaration

    Swift

    case queue(DispatchQueue)
  • Execute on the specified operation queue.

    Declaration

    Swift

    case operationQueue(OperationQueue)
  • Execute synchronously.

    Important

    If you use this option with a callback you must be prepared to handle the callback executing on any thread. This option is usually only used when creating a promise, and with callbacks is generally only suitable for running short bits of code that are thread-independent. For example you may want to use this with resolver.onRequestCancel if all you’re doing is cancelling the promise, or asking a network task to cancel, e.g.

    resolver.onRequestCancel(on: .immediate, { (_) in task.cancel() })

    Declaration

    Swift

    case immediate
  • Execute synchronously if the promise is already resolved, otherwise use another context.

    This is a convenience for a pattern where you check a promise’s result to see if it’s already resolved and only attach a callback if it hasn’t resolved yet. Passing this context to a callback will execute it synchronously before returning to the caller if and only if the promise has already resolved.

    If this is passed to a promise initializer it acts like .immediate. If passed to a DelayedPromise initializer it acts like the given context.

    Declaration

    Swift

    indirect case nowOr(PromiseContext)
  • Returns .main when accessed from the main thread, otherwise .default.

    Declaration

    Swift

    public static var auto: PromiseContext { get }
  • Returns whether a .nowOr(_:) context is executing synchronously.

    When accessed from within a callback registered with .nowOr(_:) this returns true if the callback is executing synchronously or false if it’s executing on the wrapped context. When accessed from within a callback (including Promise.init(on:_:) registered with .immediate this returns true if and only if the callback is executing synchronously and is nested within a .nowOr(_:) context that is executing synchronously. When accessed from any other scenario this always returns false.

    Remark

    The behavior of .immediate is intended to allow Promise(on: .immediate, { … }) to query the synchronous state of its surrounding scope.

    Note

    This flag will return false when executed from within DispatchQueue.main.sync nested inside a .nowOr callback, or any similar construct that blocks the current thread and runs code on another thread.

    Declaration

    Swift

    public static var isExecutingNow: Bool { get }
  • Returns the PromiseContext that corresponds to a given Dispatch QoS class.

    If the given QoS is .unspecified then .default is assumed.

    Declaration

    Swift

    public init(qos: DispatchQoS.QoSClass)
  • Returns a Boolean value indicating whether two values are equal.

    Equality is the inverse of inequality. For any values a and b, a == b implies that a != b is false.

    Declaration

    Swift

    public static func == (lhs: PromiseContext, rhs: PromiseContext) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.