Skip to content

Commit

Permalink
(Hopefully) fixed some threading issues related to merging Futures.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakehawken committed Jul 10, 2022
1 parent 994bc58 commit efc3105
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Sources/Propagate/PromiseAndFuture/Future+Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ public extension Future {
static func merge(_ futures: [Future<T, E>]) -> Future<[T], E> {
let promise = Promise<[T], E>()

futures.forEach {
$0.finally { (_) in
promise.future.lockQueue.sync {
let results = futures.compactMap { $0.result }
let failures = results.compactMap { $0.failure }
if let firstError = failures.first {
promise.reject(firstError)
}
futures.forEach { future in
future.finally { (_) in
promise.future.lockQueue.async {
guard promise.future.isComplete == false else {
return
}
let successValues = results.compactMap { $0.success }

if let errorFuture = futures.first(where: { $0.failed }),
let error = errorFuture.error {
promise.reject(error)
return
}

let successValues = futures.compactMap(\.value)
guard successValues.count == futures.count else {
return
}
Expand Down

0 comments on commit efc3105

Please sign in to comment.