Created
February 22, 2017 10:13
-
-
Save melbourne2991/89d063e3deb081b0e5da43f7eb411b61 to your computer and use it in GitHub Desktop.
rxjs fetch
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
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