Instance Methodswift-nio 2.72.0NIOCore
flatMapResult(_:)
When the current EventLoopFuture<Value>
is fulfilled, run the provided callback, which performs a synchronous computation and returns either a new value (of type NewValue
) or an error depending on the Result
returned by the closure.
@preconcurrency func flatMapResult<NewValue, SomeError>(_ body: @escaping (Value) -> Result<NewValue, SomeError>) -> EventLoopFuture<NewValue> where SomeError : Error
Parameters
- body
Function that will receive the value of this
EventLoopFuture
and return a new value or error lifted into a newEventLoopFuture
.
Returns
A future that will receive the eventual value.
Operations performed in flatMapResult
should not block, or they will block the entire event loop. flatMapResult
is intended for use when you have a data-driven function that performs a simple data transformation that can potentially error.
Other members in extension
Type members
static func andAllComplete([EventLoopFuture<Value>], on: EventLoop
) -> EventLoopFuture<Void> Returns a new
EventLoopFuture
that succeeds when all of the providedEventLoopFuture
s complete.static func andAllComplete([EventLoopFuture<Value>], promise: EventLoopPromise<Void>
) Completes a
promise
when all of the providedEventLoopFuture
s have completed.static func andAllSucceed([EventLoopFuture<Value>], on: EventLoop
) -> EventLoopFuture<Void> Returns a new
EventLoopFuture
that succeeds only if all of the provided futures succeed.static func andAllSucceed([EventLoopFuture<Value>], promise: EventLoopPromise<Void>
) Succeeds the promise if all of the provided futures succeed. If any of the provided futures fail then the
promise
will be failed – even if some futures are yet to complete.static func reduce<InputValue>(Value, [EventLoopFuture<InputValue>], on: EventLoop, @escaping (Value, InputValue) -> Value
) -> EventLoopFuture<Value> Returns a new
EventLoopFuture
that fires only when all the provided futures complete. The newEventLoopFuture
contains the result of reducing theinitialResult
with the values of the[EventLoopFuture<NewValue>]
.static func reduce<InputValue>(into: Value, [EventLoopFuture<InputValue>], on: EventLoop, @escaping (inout Value, InputValue) -> Void
) -> EventLoopFuture<Value> Returns a new
EventLoopFuture
that fires only when all the provided futures complete. The newEventLoopFuture
contains the result of combining theinitialResult
with the values of the[EventLoopFuture<NewValue>]
. This function is analogous to the standard library’sreduce(into:)
, which does not make copies of the result type for eachEventLoopFuture
.static func whenAllComplete([EventLoopFuture<Value>], on: EventLoop
) -> EventLoopFuture<[Result<Value, Error>]> Returns a new
EventLoopFuture
that succeeds when all of the providedEventLoopFuture
s complete. The newEventLoopFuture
will contain an array of results, maintaining ordering for each of theEventLoopFuture
s.static func whenAllComplete([EventLoopFuture<Value>], promise: EventLoopPromise<[Result<Value, Error>]>
) Completes a
promise
with the results of all providedEventLoopFuture
s.static func whenAllSucceed([EventLoopFuture<Value>], on: EventLoop
) -> EventLoopFuture<[Value]> Returns a new
EventLoopFuture
that succeeds only if all of the provided futures succeed. The newEventLoopFuture
will contain all of the values fulfilled by the futures.static func whenAllSucceed([EventLoopFuture<Value>], promise: EventLoopPromise<[Value]>
) Completes the
promise
with the values of allfutures
if all provided futures succeed. If any of the provided futures fail thenpromise
will be failed.static func == (lhs: EventLoopFuture, rhs: EventLoopFuture
) -> Bool
Instance members
let eventLoop: EventLoop
The
EventLoop
which is tied to theEventLoopFuture
and is used to notify all registered callbacks.func always(@escaping (Result<Value, Error>) -> Void
) -> EventLoopFuture<Value> Adds an observer callback to this
EventLoopFuture
that is called when theEventLoopFuture
has any result.func and<OtherValue>(EventLoopFuture<OtherValue>
) -> EventLoopFuture<(Value, OtherValue)> Return a new
EventLoopFuture
that succeeds when this “and” another providedEventLoopFuture
both succeed. It then provides the pair of results. If either one fails, the combinedEventLoopFuture
will fail with the first error encountered.func and<OtherValue>(value: OtherValue
) -> EventLoopFuture<(Value, OtherValue)> Return a new EventLoopFuture that contains this “and” another value. This is just syntactic sugar for
future.and(loop.makeSucceedFuture(value))
.func assertFailure(file: StaticString, line: UInt
) -> EventLoopFuture<Value> Attaches a callback to the
EventLoopFuture
that asserts the original future’s failure.func assertSuccess(file: StaticString, line: UInt
) -> EventLoopFuture<Value> Attaches a callback to the
EventLoopFuture
that asserts the original future’s success.func cascade(to: EventLoopPromise<Value>?
) Fulfills the given
EventLoopPromise
with the results from thisEventLoopFuture
.func cascadeFailure<NewValue>(to: EventLoopPromise<NewValue>?
) Fails the given
EventLoopPromise
with the error from thisEventLoopFuture
if encountered.func cascadeSuccess(to: EventLoopPromise<Value>?
) Fulfills the given
EventLoopPromise
only when thisEventLoopFuture
succeeds.func flatMap<NewValue>(@escaping (Value) -> EventLoopFuture<NewValue>
) -> EventLoopFuture<NewValue> When the current
EventLoopFuture<Value>
is fulfilled, run the provided callback, which will provide a newEventLoopFuture
.func flatMapBlocking<NewValue>(onto: DispatchQueue, @escaping (Value) throws -> NewValue
) -> EventLoopFuture<NewValue> Chain an
EventLoopFuture<NewValue>
providing the result of a IO / task that may block. For example:func flatMapError(@escaping (Error) -> EventLoopFuture<Value>
) -> EventLoopFuture<Value> When the current
EventLoopFuture<Value>
is in an error state, run the provided callback, which may recover from the error by returning anEventLoopFuture<NewValue>
. The callback is intended to potentially recover from the error by returning a newEventLoopFuture
that will eventually contain the recovered result.func flatMapErrorThrowing(@escaping (Error) throws -> Value
) -> EventLoopFuture<Value> When the current
EventLoopFuture<Value>
is in an error state, run the provided callback, which may recover from the error and returns a new value of typeValue
. The provided callback may optionallythrow
, in which case theEventLoopFuture
will be in a failed state with the new thrown error.func flatMapErrorWithEventLoop(@escaping (Error, EventLoop) -> EventLoopFuture<Value>
) -> EventLoopFuture<Value> When the current
EventLoopFuture<Value>
is in an error state, run the provided callback, which may recover from the error by returning anEventLoopFuture<NewValue>
. The callback is intended to potentially recover from the error by returning a newEventLoopFuture
that will eventually contain the recovered result.func flatMapThrowing<NewValue>(@escaping (Value) throws -> NewValue
) -> EventLoopFuture<NewValue> When the current
EventLoopFuture<Value>
is fulfilled, run the provided callback, which performs a synchronous computation and returns a new value of typeNewValue
. The provided callback may optionallythrow
.func flatMapWithEventLoop<NewValue>(@escaping (Value, EventLoop) -> EventLoopFuture<NewValue>
) -> EventLoopFuture<NewValue> When the current
EventLoopFuture<Value>
is fulfilled, run the provided callback, which will provide a newEventLoopFuture
alongside theEventLoop
associated with this future.func fold<OtherValue>([EventLoopFuture<OtherValue>], with: @escaping (Value, OtherValue) -> EventLoopFuture<Value>
) -> EventLoopFuture<Value> Returns a new
EventLoopFuture
that fires only when thisEventLoopFuture
and all the providedfutures
complete. It then provides the result of folding the value of thisEventLoopFuture
with the values of all the providedfutures
.func foldWithEventLoop<OtherValue>([EventLoopFuture<OtherValue>], with: @escaping (Value, OtherValue, EventLoop) -> EventLoopFuture<Value>
) -> EventLoopFuture<Value> Returns a new
EventLoopFuture
that fires only when thisEventLoopFuture
and all the providedfutures
complete. It then provides the result of folding the value of thisEventLoopFuture
with the values of all the providedfutures
.func get(
) async throws -> Value Get the value/error from an
EventLoopFuture
in anasync
context.func hop(to: EventLoop
) -> EventLoopFuture<Value> Returns an
EventLoopFuture
that fires when this future completes, but executes its callbacks on the target event loop instead of the original one.func map<NewValue>(@escaping (Value) -> (NewValue)
) -> EventLoopFuture<NewValue> When the current
EventLoopFuture<Value>
is fulfilled, run the provided callback, which performs a synchronous computation and returns a new value of typeNewValue
.func preconditionFailure(file: StaticString, line: UInt
) -> EventLoopFuture<Value> Attaches a callback to the
EventLoopFuture
that preconditions the original future’s failure.func preconditionSuccess(file: StaticString, line: UInt
) -> EventLoopFuture<Value> Attaches a callback to the
EventLoopFuture
that preconditions the original future’s success.func recover(@escaping (Error) -> Value
) -> EventLoopFuture<Value> When the current
EventLoopFuture<Value>
is in an error state, run the provided callback, which can recover from the error and return a new value of typeValue
. The provided callback may notthrow
, so this function should be used when the error is always recoverable.func unwrap<NewValue>(orElse: @escaping () -> NewValue
) -> EventLoopFuture<NewValue> Unwrap an
EventLoopFuture
where its type parameter is anOptional
.func unwrap<NewValue>(orError: Error
) -> EventLoopFuture<NewValue> Unwrap an
EventLoopFuture
where its type parameter is anOptional
.func unwrap<NewValue>(orReplace: NewValue
) -> EventLoopFuture<NewValue> Unwrap an
EventLoopFuture
where its type parameter is anOptional
.func wait(file: StaticString, line: UInt
) throws -> Value Wait for the resolution of this
EventLoopFuture
by blocking the current thread until it resolves.func whenComplete(@escaping (Result<Value, Error>) -> Void
) Adds an observer callback to this
EventLoopFuture
that is called when theEventLoopFuture
has any result.func whenCompleteBlocking(onto: DispatchQueue, @escaping (Result<Value, Error>) -> Void
) Adds an observer callback to this
EventLoopFuture
that is called when theEventLoopFuture
has any result. The observer callback is permitted to block.func whenFailure(@escaping (Error) -> Void
) Adds an observer callback to this
EventLoopFuture
that is called when theEventLoopFuture
has a failure result.func whenFailureBlocking(onto: DispatchQueue, @escaping (Error) -> Void
) Adds an observer callback to this
EventLoopFuture
that is called when theEventLoopFuture
has a failure result. The observer callback is permitted to block.func whenSuccess(@escaping (Value) -> Void
) Adds an observer callback to this
EventLoopFuture
that is called when theEventLoopFuture
has a success result.func whenSuccessBlocking(onto: DispatchQueue, @escaping (Value) -> Void
) Adds an observer callback to this
EventLoopFuture
that is called when theEventLoopFuture
has a success result. The observer callback is permitted to block.
Show obsolete interfaces (9)
Hide obsolete interfaces
func and<OtherValue>(EventLoopFuture<OtherValue>, file: StaticString, line: UInt
) -> EventLoopFuture<(Value, OtherValue)> func and<OtherValue>(value: OtherValue, file: StaticString, line: UInt
) -> EventLoopFuture<(Value, OtherValue)> func flatMap<NewValue>(file: StaticString, line: UInt, @escaping (Value) -> EventLoopFuture<NewValue>
) -> EventLoopFuture<NewValue> func flatMapError(file: StaticString, line: UInt, @escaping (Error) -> EventLoopFuture<Value>
) -> EventLoopFuture<Value> func flatMapErrorThrowing(file: StaticString, line: UInt, @escaping (Error) throws -> Value
) -> EventLoopFuture<Value> func flatMapResult<NewValue, SomeError>(file: StaticString, line: UInt, @escaping (Value) -> Result<NewValue, SomeError>
) -> EventLoopFuture<NewValue> func flatMapThrowing<NewValue>(file: StaticString, line: UInt, @escaping (Value) throws -> NewValue
) -> EventLoopFuture<NewValue> func map<NewValue>(file: StaticString, line: UInt, @escaping (Value) -> (NewValue)
) -> EventLoopFuture<NewValue> func recover(file: StaticString, line: UInt, @escaping (Error) -> Value
) -> EventLoopFuture<Value>