@@ -43,6 +43,40 @@ private func _when<U: Thenable>(_ thenables: [U]) -> Promise<Void> {
4343 return rp
4444}
4545
46+ private func __when< T> ( _ guarantees: [ Guarantee < T > ] ) -> Guarantee < Void > {
47+ var countdown = guarantees. count
48+ guard countdown > 0 else {
49+ return . value( Void ( ) )
50+ }
51+
52+ let rg = Guarantee < Void > ( . pending)
53+
54+ #if PMKDisableProgress || os(Linux)
55+ var progress : ( completedUnitCount: Int , totalUnitCount: Int ) = ( 0 , 0 )
56+ #else
57+ let progress = Progress ( totalUnitCount: Int64 ( guarantees. count) )
58+ progress. isCancellable = false
59+ progress. isPausable = false
60+ #endif
61+
62+ let barrier = DispatchQueue ( label: " org.promisekit.barrier.when " , attributes: . concurrent)
63+
64+ for guarantee in guarantees {
65+ guarantee. pipe { ( _: T ) in
66+ barrier. sync ( flags: . barrier) {
67+ guard rg. isPending else { return }
68+ progress. completedUnitCount += 1
69+ countdown -= 1
70+ if countdown == 0 {
71+ rg. box. seal ( ( ) )
72+ }
73+ }
74+ }
75+ }
76+
77+ return rg
78+ }
79+
4680/**
4781 Wait for all promises in a set to fulfill.
4882
@@ -357,7 +391,12 @@ public func when(_ guarantees: Guarantee<Void>...) -> Guarantee<Void> {
357391 return when ( guarantees: guarantees)
358392}
359393
360- // Waits on all provided Guarantees.
394+ /// Waits on all provided Guarantees.
361395public func when( guarantees: [ Guarantee < Void > ] ) -> Guarantee < Void > {
362396 return when ( fulfilled: guarantees) . recover { _ in } . asVoid ( )
363397}
398+
399+ /// Waits on all provided Guarantees.
400+ public func when< U, V> ( guarantees gu: Guarantee < U > , _ gv: Guarantee < V > ) -> Guarantee < ( U , V ) > {
401+ return __when ( [ gu. asVoid ( ) , gv. asVoid ( ) ] ) . map ( on: nil ) { ( gu. value!, gv. value!) }
402+ }
0 commit comments