flatMap(_:)
When the current EventLoopFuture<Value>
is fulfilled, run the provided callback, which will provide a new EventLoopFuture
.
func flatMap<NewValue>(_ callback: @escaping (Value) -> EventLoopFuture<NewValue>) -> EventLoopFuture<NewValue>.Isolated where NewValue : Sendable
Parameters
- callback
Function that will receive the value of this
EventLoopFuture
and return a newEventLoopFuture
.
Returns
A future that will receive the eventual value.
This allows you to dynamically dispatch new asynchronous tasks as phases in a longer series of processing steps. Note that you can use the results of the current EventLoopFuture<Value>
when determining how to dispatch the next operation.
This works well when you have APIs that already know how to return EventLoopFuture
s. You can do something with the result of one and just return the next future:
let d1 = networkRequest(args).future()
let d2 = d1.flatMap { t -> EventLoopFuture<NewValue> in
. . . something with t . . .
return netWorkRequest(args)
}
d2.whenSuccess { u in
NSLog("Result of second request: \(u)")
}
Note that the returned EventLoopFuture
still needs a Sendable
wrapped value, as it may have been created on a different event loop.
Note: In a sense, the EventLoopFuture<NewValue>
is returned before it’s created.