Created
January 28, 2017 23:19
-
-
Save fmo91/a4edbdb1a04f2bbf65da34183d2fc5bb to your computer and use it in GitHub Desktop.
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
| func getUser(withID id: Int) -> Future<User> { | |
| return Future { completion in | |
| // Here you call an API | |
| // that returns the user you are looking for. | |
| APIClient.get(getUserURL(id: id)) { user in | |
| completion(.success(user)) | |
| } | |
| } | |
| } | |
| func getPosts(for username: String) -> Future<[Post]> { | |
| return Future { completion in | |
| APIClient.get(getPostsURL(username: username)) { posts in | |
| completion(.success(posts)) | |
| } | |
| } | |
| } | |
| // And in your view controller... | |
| getUser(withID: 4) | |
| .flatMap(getPosts) | |
| .map({ posts in | |
| return posts.map { post in | |
| return post.title | |
| } | |
| }) | |
| .subscribe(onNext: { postsTitles in | |
| print(postsTitles) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment