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

Tasks with different result type in parallel #45

Open
schartyom opened this issue Nov 10, 2016 · 4 comments
Open

Tasks with different result type in parallel #45

schartyom opened this issue Nov 10, 2016 · 4 comments

Comments

@schartyom
Copy link

If there any workaround to run tasks with different result type in parallel?
Task.whenAll([Task(1), Task("two")]) - This won't compile

@xxtesaxx
Copy link

Task.whenAll([Task(1 as AnyObject), Task("two" as AnyObject)]) at least compiles for me

@serejahh
Copy link

serejahh commented Dec 16, 2016

@schartyom you should have a look at PromiseKit. They support when with several different types: https://github.com/mxcl/PromiseKit/blob/master/Sources/when.swift#L95. With Bolts you can't do so

@schartyom
Copy link
Author

schartyom commented Dec 21, 2016

I've created extension to make any task to be Task<Void>

extension Task {
    func emptyResultTask() -> Task<Void> {
        return continueWithTask { task -> Task<Void> in
            if let error = task.error {
                return Task<Void>(error: error)
            } else if task.cancelled {
                return Task<Void>.cancelledTask()
            } else {
                return Task.emptyTask()
            }
        }
    }
}

Now you can run any task in parallel: Task.whenAll([Task(1).emptyResultTask(), Task("two").emptyResultTask()])
Waiting for covariant generics in Swift :)

@serejahh
Copy link

It's a hack :) Usually it makes sense to get the exact results, not just notifying about finishing

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

3 participants