Thomvis / BrightFutures

Write great asynchronous code in Swift using futures and promises
MIT License
1.9k stars 184 forks source link

'continueWithBlock' method #130

Closed rcbello closed 8 years ago

rcbello commented 8 years ago

Is there a way to chain two futures without requiring the first future to either fail or succeed? much like continueWithBlock method on BFTasks?

Thomvis commented 8 years ago

No, there is way to chain futures regardless of their success/failure. I'm not sure if there is a big use case for that. With BFTasks, I often ended up writing an if statement in the continueWithBlock to check for the error. I assume that most cases can be handled more elegantly with a combination of recover/recoverWith and map/flatMap. But if you think this is not the case, I'd like to see an example!

rcbello commented 8 years ago

Yeah, I too realized there's probably few use case for it. My own was, I wanted to make sure the dismissal of a view controller, which is called upon the completion of a future, is finished before I can do something with the result of the said future. I just added an extension on AsyncType to help me with it:

extension AsyncType {
    func continueWith<AnotherAsync: AsyncType>(f: (Value) -> AnotherAsync) -> AnotherAsync {
        return AnotherAsync { complete in
            onComplete(ImmediateExecutionContext) { (result) in
                f(result).onComplete(ImmediateExecutionContext, callback: { (finalResult) in
                    complete(finalResult)
                })
            }
        }
    }
}