Suppose we've three Futures each wrapping an asynchronous call & each depends on the previous Promise to be fulfilled. Ideally, I'd like to have all these get executed only when the subscriber is attached. How do we get this going using Combine?
private func future1() -> Future<Bool, Error> {
return Future { [weak self] promise in
self?.task1 { result in
promise(result)
}
}
}
private func future2() -> Future<Bool, Error> {
return Future { [weak self] promise in
self?.task2 { result in
promise(result)
}
}
}
private func future3() -> Future<Bool, Error> {
return Future { [weak self] promise in
self?.task3 { result in
promise(result)
}
}
}
future1()..
future2()..
future3()..
.sink {..}
Futureexecutes its closure synchronously, so it's usually wrapped in aDeferred, which waits for a subscription before it executes its closure.This sounds like exactly what you'd need to do.
Then, you can use
.flatMapto stagger each publisher: