Last active
January 19, 2023 08:42
-
-
Save arashkashi/95dddf5312941f02ca0973109433a1d2 to your computer and use it in GitHub Desktop.
multiple BE calls for fetching data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewModel: ObservableObject { | |
var bag = Set<AnyCancellable>() | |
var t = "hellow" | |
init() { | |
let mainPublisher = [3, 2, 1].publisher | |
mainPublisher.flatMap({ value in | |
printit(value).map { calcualted in | |
return (value, calcualted) | |
} | |
}) | |
.collect() | |
.map({ result in | |
print(result) | |
return result | |
}) | |
.sink(receiveValue: { _ in | |
}) | |
.store(in: &bag) | |
} | |
} | |
func printit(_ value: Int) -> Future<Int, Never> { | |
return Future { promise in | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(value)) { | |
print("printit -> \(value)") | |
promise(.success(value * 2)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment