mirror of
https://github.com/apple/swift-nio
synced 2026-11-29 05:24:21 +00:00
add ELF.whenAllSucceed method to act as "fail fast" counter-part to ELF.whenAllComplete (#804)
Motivation:
`ELF.whenAllComplete` is a "fail slow" method that returns an `EventLoopFuture` that always succeeds after all provided futures complete - fulfilling with all the results of the futures.
`ELF.whenAllSucceed` is a "fail fast" method that returns an `EventLoopFuture` that succeeds if all of the futures succeed - and returning the fulfilled values.
This new method acts as a counterpart to both `ELF.whenAllComplete` and `andAll`
Modifications:
Add `ELF.whenAllSucceed` that returns an EventLoopFuture with all of the values provided from the futures being awaited.
Rename `ELF.whenAllComplete(_:eventLoop:)` to `ELF.whenAllComplete(_🔛)` to match new `whenAllSucceed` and Swift API guidelines
Result:
New method for flattening multiple futures into a single EventLoopFuture that fulfills with an array of their success values if they all succeed.
This commit is contained in:
1 parent
6ca8c0e189
commit
9b9dcebecf
2 files changed
+21
-5
No files matched your search
@@ -986,7 +986,21 @@ extension EventLoopFuture {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: whenAll
|
||||
|
||||
extension EventLoopFuture {
|
||||
/// Returns a new `EventLoopFuture` that succeeds only if all of the provided futures succeed.
|
||||
/// The new `EventLoopFuture` will contain all of the values fulfilled by the futures.
|
||||
///
|
||||
/// The returned `EventLoopFuture` will fail as soon as any of the futures fails.
|
||||
/// - Parameters:
|
||||
/// - futures: An array of homogenous `EventLoopFuture`s to wait on for fulfilled values.
|
||||
/// - on: The `EventLoop` on which the new `EventLoopFuture` callbacks will fire.
|
||||
/// - Returns: A new `EventLoopFuture` with all of the values fulfilled by the provided futures.
|
||||
public static func whenAllSucceed(_ futures: [EventLoopFuture<Value>], on eventLoop: EventLoop) -> EventLoopFuture<[Value]> {
|
||||
return .reduce(into: [], futures, eventLoop: eventLoop) { (results, value) in results.append(value) }
|
||||
}
|
||||
|
||||
/// Returns a new `EventLoopFuture` that succeeds when all of the provided `EventLoopFuture`s complete.
|
||||
/// The new `EventLoopFuture` will contain an array of results, maintaining ordering for each of the `EventLoopFuture`s.
|
||||
///
|
||||
@@ -994,10 +1008,12 @@ extension EventLoopFuture {
|
||||
///
|
||||
/// If it is desired to flatten them into a single `EventLoopFuture` that fails on the first `EventLoopFuture` failure,
|
||||
/// use one of the `reduce` methods instead.
|
||||
/// - Parameter futures: An array of homogenous `EventLoopFuture`s to gather results from.
|
||||
/// - Parameters:
|
||||
/// - futures: An array of homogenous `EventLoopFuture`s to gather results from.
|
||||
/// - on: The `EventLoop` on which the new `EventLoopFuture` callbacks will fire.
|
||||
/// - Returns: A new `EventLoopFuture` with all the results of the provided futures.
|
||||
public static func whenAllComplete(_ futures: [EventLoopFuture<Value>],
|
||||
eventLoop: EventLoop) -> EventLoopFuture<[Result<Value, Error>]> {
|
||||
on eventLoop: EventLoop) -> EventLoopFuture<[Result<Value, Error>]> {
|
||||
let promise = eventLoop.makePromise(of: [Result<Value, Error>].self)
|
||||
|
||||
if eventLoop.inEventLoop {
|
||||
|
||||
@@ -869,7 +869,7 @@ class EventLoopFutureTest : XCTestCase {
|
||||
let future = EventLoopFuture.whenAllComplete([
|
||||
group.next().makeFailedFuture(EventLoopFutureTestError.example),
|
||||
group.next().makeSucceededFuture(true)
|
||||
], eventLoop: group.next())
|
||||
], on: group.next())
|
||||
XCTAssertNoThrow(try future.wait())
|
||||
}
|
||||
|
||||
@@ -885,7 +885,7 @@ class EventLoopFutureTest : XCTestCase {
|
||||
group.next().makeSucceededFuture(10),
|
||||
group.next().makeFailedFuture(EventLoopFutureTestError.example),
|
||||
group.next().makeSucceededFuture(5)
|
||||
], eventLoop: group.next()).wait()
|
||||
], on: group.next()).wait()
|
||||
|
||||
XCTAssertEqual(try results[0].get(), 3)
|
||||
XCTAssertThrowsError(try results[1].get())
|
||||
@@ -906,7 +906,7 @@ class EventLoopFutureTest : XCTestCase {
|
||||
var succeeded = false
|
||||
var completedPromises = false
|
||||
|
||||
let mainFuture = EventLoopFuture.whenAllComplete(futures, eventLoop: group.next())
|
||||
let mainFuture = EventLoopFuture.whenAllComplete(futures, on: group.next())
|
||||
mainFuture.whenSuccess { _ in
|
||||
XCTAssertTrue(completedPromises)
|
||||
XCTAssertFalse(succeeded)
|
||||
|
||||
Reference in new issue
Block a user