Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. getObservableFromFetch(url){
  2. //Create and return an Observable.
  3. return Observable.create(observer => {
  4. //Make use of Fetch API to get data from URL
  5. fetch(url)
  6. .then(res => {
  7. /*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.*/
  8. return res.json();
  9. })
  10. .then(body => {
  11. observer.next(body);
  12. /*Complete the Observable as it won't produce any more event */
  13. observer.complete();
  14. })
  15. //Handle error
  16. .catch(err => observer.error(err));
  17. })
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement