Skip to content

Instantly share code, notes, and snippets.

@Athosone
Last active October 2, 2018 17:36
Show Gist options
  • Save Athosone/934b0dacafc71727d50940d9516505c4 to your computer and use it in GitHub Desktop.
Save Athosone/934b0dacafc71727d50940d9516505c4 to your computer and use it in GitHub Desktop.
RxSwift+fromAsync example
// Consider this function:
func fetchPost(by postId: Int, completion: @escaping (Post?, Error?) -> Void) {
...
}
// Using our extension to use an observable
// fetchPostObservable looks like: fetchPostObservable(postId) -> Observable<Post>
let fetchPostObservable = Observable.fromAsync(postService.getPost(postId:completion:))
// Now to fetch a post you just have to call it just like you would call any function which returns an Observable:
fecthPostObservable(10).subscribe(onNext: { (post) in
print("Fetching post with id 10 succeeded")
}, onError: { (error) in
print("Fetching post failed: \(error)"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment