Created
August 20, 2020 19:48
-
-
Save iMostfa/af1c328c3383c67f3a5fc1ffd04435f1 to your computer and use it in GitHub Desktop.
flatMap without losing the value from upstream
This file contains 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
import Combine | |
var moviessPublisher = ["James bond","abo elaraby","F.R.I.E.N.D.S","House Of Cards"] | |
.publisher | |
.eraseToAnyPublisher() | |
func rating(for movie: String) -> AnyPublisher<Int,Never> { //could be a restful api that takes movie name and return the rating | |
return Just((movie.count * 2) % 5).eraseToAnyPublisher() | |
} | |
//somewhere else where we want to load movies, and also, get rating for each movie | |
moviessPublisher | |
.flatMap { (movieName) in | |
return rating(for: movieName) | |
.zip(Just(movieName)) | |
} | |
.sink { (data) in | |
print("movie name is \(data.1) and movie rating is \(data.0)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment