In Combine there are (among others) these two publishers:
let just = Just("Just")
let future = Future<String, Never>({ (promise) in
DispatchQueue.main.async {
promise(.success("Future"))
}
})
These two have a different output when put into a combineLatest publisher like in the following
just.combineLatest([1,2,3].publisher).sink { (value) in
print(value)
}.store(in: &bin)
/* prints
("Just", 1)
("Just", 2)
("Just", 3)
*/
future.combineLatest([1,2,3].publisher).sink { (value) in
print(value)
}.store(in: &bin)
/* prints:
("Future", 3)
*/
Is there any way to modify (e.g. an operator) the Future publisher in a way so that it will behave like the Just publisher?
Futuredoesn't behave differently when you callcombineLateston it. Your example is simply flawed due to the async dispatch you add inside yourFuture.combineLatestonly emits a value when all of its upstreams emitted a value. So if you add aDispatchQueue.main.asyncbefore you calledpromiseinside yourFuture, you'll delay the emission in yourFuture. Because of this,Futurewill emit later than theArray.publisherand hence you'll only see 1 (or sometimes 0, depending on when the next runloop on the main thread is executed) outputs fromcombineLatest.As soon as you remove the async call, you see the same output from
FutureandJust.