Skip to content

Latest commit

 

History

History
62 lines (51 loc) · 2.1 KB

operators-and-ajax.md

File metadata and controls

62 lines (51 loc) · 2.1 KB

#Operators and Ajax There is an ajax operator on the Rx object.

Using the ajax() operator

index.html

<html>
  <body>
    <div id="result">
    </div>
    <script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script>
        <script src="app.js"></script>
  </body>
</html>

app.js

import { ajax } from 'rxjs/ajax';
import { map } from 'rxjs/operators';

let person$ = .ajax({
      url : 'http://swapi.co/api/people/1',
      crossDomain: true, 
      createXHR: function () {
        return new XMLHttpRequest();
     }
    })
  .pipe(
    map(e => e.response)
  );

const subscription = person$
  .subscribe(res => {
    let element = document.getElementById('result');
    element.innerHTML = res.name
    console.log(res)
  });
  

A little GOTCHA from this is how we call the ajax() operator, we obviously specify a bunch of stuff other thant the url property. The reason for this is that the ajax operator does the following :

default factory of XHR in ajaxObservable sets withCredentials to true by default

So we give at a custom factory and it works. I understand this is an issue that is currently looked upon

Using fetch API

const fetchSubscription = Rx.Observable
.from(fetch('http://swapi.co/api/people/1'))
.flatMap((res) => Rx.Observable.from(res.json()) )
.subscribe((fetchRes) => {
    console.log('fetch sub', fetchRes);
})

So a couple of things here happens worth mentioning

  • fetch api is promised base, however using .from() Rxjs allows us to insert promise as a parameter and converts it to an Observable.
  • BUT the result coming back is a response object that we need to convert to Json. Calling json() will do that for you but that operation returns a Promise. So we need to use another from() operator. But creating an Observable inside an observable creates a list of observables and we can't have that, we want Json. So we use an operator called flatMap() to fix that. Read more on flatMap() here

And finally we get the Json we expect, no issues with CORS but a little more to write.