Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getObservableFromFetch(url){
- //Create and return an Observable.
- return Observable.create(observer => {
- //Make use of Fetch API to get data from URL
- fetch(url)
- .then(res => {
- /*The response.json() doesn't return json, it returns a "readable stream" which is a promise which needs to be resolved to get the actual data.*/
- return res.json();
- })
- .then(body => {
- observer.next(body);
- /*Complete the Observable as it won't produce any more event */
- observer.complete();
- })
- //Handle error
- .catch(err => observer.error(err));
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement