I have an array of values I map to multiple promises that give me each a EventLoopFuture. So I end up with a method that has a variable-size [EventLoopFuture], and I need all the responses to succeed before I can continue. If one or more of them returns an error, I need to perform the error scenario.
How can I await the entire [EventLoopFuture] to complete before continuing with either the succeed path or error path?
EventLoopFuture
's got a reduce(into: ...)
method that can be used quite well for that purpose (and other tasks where you want to accumulate multiple values):
let futureOfStrings: EventLoopFuture<[String]> =
EventLoopFuture<String>.reduce(into: Array<String>(),
futures: myArrayFutureStrings,
on: someEventLoop,
{ array, nextValue in array.append(nextValue) })
To specifically turn [EventLoopFuture<Something>]
into EventLoopFuture<[Something]>
you can also use the shorter whenAllSucceed
let futureOfStrings: EventLoopFuture<[String]> =
EventLoopFuture<String>.whenAllSucceed(myStringFutures, on: someEventLoop)