Skip to content

Instantly share code, notes, and snippets.

@fmo91
Created January 28, 2017 23:19
Show Gist options
  • Select an option

  • Save fmo91/a4edbdb1a04f2bbf65da34183d2fc5bb to your computer and use it in GitHub Desktop.

Select an option

Save fmo91/a4edbdb1a04f2bbf65da34183d2fc5bb to your computer and use it in GitHub Desktop.
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