Skip to content

Instantly share code, notes, and snippets.

@arashkashi
Last active January 19, 2023 08:42
Show Gist options
  • Save arashkashi/95dddf5312941f02ca0973109433a1d2 to your computer and use it in GitHub Desktop.
Save arashkashi/95dddf5312941f02ca0973109433a1d2 to your computer and use it in GitHub Desktop.
multiple BE calls for fetching data
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