FutureProtocol
public protocol FutureProtocol : CustomDebugStringConvertible, CustomReflectable
A future models reading a value which may become available at some point.
A FutureProtocol may be preferable to an architecture using completion
handlers; separating the mechanism for handling the completion from the call
that began it leads to a more readable code flow.
A future is primarily useful as a joining mechanism for asynchronous
operations. Though the protocol requires a synchronous accessor, its use is
not recommended outside of testing. upon is preferred for nearly all access:
myFuture.upon(.main) { value in
print("I now have the value: \(value)")
}
FutureProtocol makes no requirement on conforming types regarding thread-safe
access, though ideally all members of the future could be called from any
thread.
-
A type that represents the result of some asynchronous operation.
Declaration
Swift
associatedtype Value -
upon(_:execute:)Default implementationCall some
bodyclosure once the value is determined.If the value is determined, the closure should be submitted to the
executorimmediately.Default Implementation
Call some
bodyclosure in the background once the value is determined.If the value is determined, the closure will be enqueued immediately, but this call is always asynchronous.
Declaration
Swift
func upon(_ executor: Executor, execute body: @escaping(Value) -> Void) -
peek()Default implementationChecks for and returns a determined value.
An implementation should use a
best effort
to return this value and not unnecessarily block in order to to return.Default Implementation
By default, calls
waitwith no delay.Declaration
Swift
func peek() -> Value?Return Value
The determined value, if already filled, or
nil. -
Waits synchronously for the value to become determined.
If the value is already determined, the call returns immediately with the value.
Declaration
Swift
func wait(until time: DispatchTime) -> Value?Parameters
timeA deadline for the value to be determined.
Return Value
The determined value, if filled within the timeout, or
nil.
-
debugDescriptionExtension methodA textual representation of this instance, suitable for debugging.
Declaration
Swift
public var debugDescription: String { get } -
customMirrorExtension methodReturn the
Mirrorforself.Declaration
Swift
public var customMirror: Mirror { get }
-
andThen(upon:start:)Extension methodBegins another asynchronous operation by passing the deferred value to
requestNextValueonce it becomes determined.andThenis similar tomap, butrequestNextValuereturns another future instead of an immediate value. UseandThenwhen you want the reciever to feed into another asynchronous operation. You might hear this referred to aschaining
orbinding
.Declaration
Swift
public func andThen<NewFuture: FutureProtocol>(upon executor: PreferredExecutor, start requestNextValue: @escaping(Value) -> NewFuture) -> Future<NewFuture.Value> -
andThen(upon:start:)Extension methodBegins another asynchronous operation by passing the deferred value to
requestNextValueonce it becomes determined.andThenis similar tomap, butrequestNextValuereturns another future instead of an immediate value. UseandThenwhen you want the reciever to feed into another asynchronous operation. You might hear this referred to aschaining
orbinding
.Note
It is important to keep in mind the thread safety of the
requestNextValueclosure. Creating a new asynchronous task typically involves state. Ensure the function is compatible withexecutor.Declaration
Parameters
executorContext to execute the transformation on.
requestNextValueStart a new operation with the future value.
Return Value
The new deferred value returned by the
transform.
-
and(_:)Extension method -
and(_:_:)Extension methodReturns a value that becomes determined after the callee and both other futures become determined.
See
SequenceType.allFilled() -
and(_:_:_:)Extension methodReturns a value that becomes determined after the callee and all other futures become determined.
See
SequenceType.allFilled() -
and(_:_:_:_:)Extension methodReturns a value that becomes determined after the callee and all other futures become determined.
See
SequenceType.allFilled()Declaration
-
and(_:_:_:_:_:)Extension methodReturns a value that becomes determined after the callee and all other futures become determined.
See
SequenceType.allFilled()Declaration
Swift
public func and<Other1, Other2, Other3, Other4, Other5>(_ one: Other1, _ two: Other2, _ three: Other3, _ four: Other4, _ five: Other5) -> Future<(Value, Other1.Value, Other2.Value, Other3.Value, Other4.Value, Other5.Value)> where Other1 : FutureProtocol, Other2 : FutureProtocol, Other3 : FutureProtocol, Other4 : FutureProtocol, Other5 : FutureProtocol -
and(_:_:_:_:_:_:)Extension methodReturns a value that becomes determined after the callee and all other futures become determined.
See
SequenceType.allFilled()Declaration
Swift
public func and<Other1, Other2, Other3, Other4, Other5, Other6>(_ one: Other1, _ two: Other2, _ three: Other3, _ four: Other4, _ five: Other5, _ six: Other6) -> Future<(Value, Other1.Value, Other2.Value, Other3.Value, Other4.Value, Other5.Value, Other6.Value)> where Other1 : FutureProtocol, Other2 : FutureProtocol, Other3 : FutureProtocol, Other4 : FutureProtocol, Other5 : FutureProtocol, Other6 : FutureProtocol -
and(_:_:_:_:_:_:_:)Extension methodReturns a value that becomes determined after the callee and all other futures become determined.
See
SequenceType.allFilled()Declaration
Swift
public func and<Other1, Other2, Other3, Other4, Other5, Other6, Other7>(_ one: Other1, _ two: Other2, _ three: Other3, _ four: Other4, _ five: Other5, _ six: Other6, _ seven: Other7) -> Future<(Value, Other1.Value, Other2.Value, Other3.Value, Other4.Value, Other5.Value, Other6.Value, Other7.Value)> where Other1 : FutureProtocol, Other2 : FutureProtocol, Other3 : FutureProtocol, Other4 : FutureProtocol, Other5 : FutureProtocol, Other6 : FutureProtocol, Other7 : FutureProtocol
-
every(per:)Extension methodReturns a future that transparently performs the
eachUseTransformwhile reusing the original future.The
upon(_:execute:)andwait(until:)methods of the returned future forward toself, wrapping access to the underlying value by the transform.Though producing similar results, this method does not work like
map, which eagerly evaluates the transform to create a new future with new storage. This is not suitable for simple transforms, such as unboxing or conversion.See
map(upon:transform:)Declaration
Swift
public func every<NewValue>(per eachUseTransform: @escaping(Value) -> NewValue) -> Future<NewValue>
-
map(upon:transform:)Extension methodReturns a future containing the result of mapping
transformover the deferred value.Declaration
Swift
public func map<NewValue>(upon executor: PreferredExecutor, transform: @escaping(Value) -> NewValue) -> Future<NewValue> -
map(upon:transform:)Extension methodReturns a future containing the result of mapping
transformover the deferred value.mapsubmits thetransformto theexecutoronce the future’s value is determined.Declaration
Parameters
executorContext to execute the transformation on.
transformCreates something using the deferred value.
Return Value
A new future that is filled once the receiver is determined.
-
defaultUponExecutorExtension methodThe executor to use as a default argument to
uponmethods onFuture.Don’t provide a default parameter using this declaration unless doing so is unambiguous. For instance,
mapandandThenonce had a default executor, but users found it unclear wherethe handlers executed.Declaration
Swift
public static var defaultUponExecutor: PreferredExecutor { get }
View on GitHub
Install in Dash
FutureProtocol Protocol Reference