Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference between continueWith and continueWithTask not clear in the documentation #53

Open
ir-fuel opened this issue Mar 9, 2017 · 1 comment

Comments

@ir-fuel
Copy link

ir-fuel commented Mar 9, 2017

The docs state:

If you return any object from continueWith function - this will become a result of the new function. And if you want to call into more tasks and return those results - you can use continueWithTask. This gives you an ability to chain more asynchronous work together.

For me the difference is absolutely not clear, especially since both have exactly the same signature in Swift.

    @discardableResult
    public func continueWith<S>(_ executor: Executor = .default, continuation: @escaping ((Task) throws -> S)) -> Task<S> {

vs

    @discardableResult
    public func continueWithTask<S>(_ executor: Executor = .default, continuation: @escaping ((Task) throws -> Task<S>)) -> Task<S> {

in the source code for continueWith it states

- returns: A task that will be completed with a result from a given closure.

continueWithTask has

- returns: A task that will be completed when a task returned from a closure is completed.

Does this mean that the task returned by continueWith will have a completion/error state as soon as it is returned, while in continueWithTask it might not immediately be the case?

And in the second case, is the task that will be completed not simply the task returned by the closure?

@xxtesaxx
Copy link

As far as I'm concerned, you use continueWith if you want to do something with the result of a task and return a value to be used by the next continuation block. For example, the previous task returns a string, you manipulate the string in continueWith and return the result. Then in the next block something is done with the manipulated string.

continueWithTask on the other side is used if you instead of returning a plain value, you want to return another task. Chaining another continueWith will get the result of the other task. Here is an example:

aFunctionWhichReturnsATaskOfInt().continueOnSuccessWith(){ aInt in
return result > 0
}.continueOnSuccessWithTask(){ aBool -> Task in
return anotherFunctionWhichReturnsATaskOfString(aBool: aBool)
}.continueOnSuccessWith(){ aString in
...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants