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.maincontext guarantee that they all execute within the same run loop pass. This means UI manipulations in chained callbacks on.mainwill 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
.backgroundQoS.Declaration
Swift
case background -
Execute on a dispatch queue with the
.utilityQoS.Declaration
Swift
case utility -
Execute on a dispatch queue with the
.defaultQoS.Declaration
Swift
case `default` -
Execute on a dispatch queue with the
.userInitiatedQoS.Declaration
Swift
case userInitiated -
Execute on a dispatch queue with the
.userInteractiveQoS.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.onRequestCancelif 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
resultto 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 aDelayedPromiseinitializer it acts like the given context.Declaration
Swift
indirect case nowOr(PromiseContext) -
Returns whether a
.nowOr(_:)context is executing synchronously.When accessed from within a callback registered with
.nowOr(_:)this returnstrueif the callback is executing synchronously orfalseif it’s executing on the wrapped context. When accessed from within a callback (includingPromise.init(on:_:)registered with.immediatethis returnstrueif 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 returnsfalse.Remark
The behavior of
.immediateis intended to allowPromise(on: .immediate, { … })to query the synchronous state of its surrounding scope.Note
This flag will return
falsewhen executed from withinDispatchQueue.main.syncnested inside a.nowOrcallback, 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
PromiseContextthat corresponds to a given Dispatch QoS class.If the given QoS is
.unspecifiedthen.defaultis 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
aandb,a == bimplies thata != bisfalse.Declaration
Swift
public static func == (lhs: PromiseContext, rhs: PromiseContext) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
View on GitHub
PromiseContext Enumeration Reference