Why does Future type need to call attempToFulfill immediately in Combine?

I'm wondering why Future doesn't defer attempToFulfill to reduce the cost when initializing.

In the following example, the console will print "Hi" even there is no subscriber is sinking values.

import Combine

let future = Future<Int, Never> { _ in print("Hi") }

I think Future type should start resolving a result for subscribers only when they request(_ demand:) and the requested demand is greater than zero. Then we don't need to pay the cost for generating a future object.

This is a standard behaviour of most popular future libraries, e.g. BrightFutures and PromiseKit. The benefit is that you don't have to manually retain the future for it to execute, it will either execute immediately or will be retained by a queue it's currently scheduled on. And I do quite like the fact that Combine follows this behavior, this way it's so much easier to migrate from any of those libraries to Combine/OpenCombine.

Besides, you can always wrap your Future with Deferred if needed.

2 Likes

Cool, now I got it.

let defferred = Deferred {
    
    Future<Int, Never> { _ in print("Hi") }
    
}
2 Likes
Terms of Service

Privacy Policy

Cookie Policy