How to convert from callback to Future Publisher in Combine

Issue #527

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import Foundation
import Combine

public typealias TaskCompletion = (Result<(), Error>) -> Void

public protocol Task: AnyObject {
var name: String { get }
func run(workflow: Workflow, completion: TaskCompletion)
}

public extension Task {
func asPublisher(workflow: Workflow) -> AnyPublisher<(), Error> {
return Future({ completion in
self.run(workflow: workflow, completion: completion)
}).eraseToAnyPublisher()
}
}

let sequence = Publishers.Sequence<[AnyPublisher<(), Error>], Error>(
sequence: tasks.map({ $0.asPublisher(workflow: self) })
)

Comments