Skip to content

Instantly share code, notes, and snippets.

@melbourne2991
Created February 22, 2017 10:13
Show Gist options
  • Save melbourne2991/89d063e3deb081b0e5da43f7eb411b61 to your computer and use it in GitHub Desktop.
Save melbourne2991/89d063e3deb081b0e5da43f7eb411b61 to your computer and use it in GitHub Desktop.
rxjs fetch
const storiesEndpoint = 'https://hacker-news.firebaseio.com/v0/topstories.json?limit=10';
const storiesFetch$ = Rx.Observable.fromPromise(fetch(storiesEndpoint));
const decoder = new TextDecoder();
const storyIds$ = storiesFetch$
.map(response => response.body)
.map(body => body.getReader())
.switchMap((reader) => {
return Rx.Observable.create((observer) => {
function search() {
reader.read().then((data) => {
if(!data.done) {
observer.next(data.value);
return search();
}
observer.complete();
})
}
search();
})
}).subscribe((val) => {
console.log('val >', val);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment