Skip to content

Instantly share code, notes, and snippets.

@jackinf
Created April 18, 2019 11:43
Show Gist options
  • Save jackinf/147348fe611f7fba4eeb58ccaee8a984 to your computer and use it in GitHub Desktop.
Save jackinf/147348fe611f7fba4eeb58ccaee8a984 to your computer and use it in GitHub Desktop.
Reactive Programming snippets

RxJS personal examples

Two consequent dependent API calls

const { XMLHttpRequest } = require('xmlhttprequest');
const { ajax } = require('rxjs/ajax');
const { mergeMap } = require('rxjs/operators/mergeMap');

const config = {
  crossDomain: true,
  createXHR: function () {
    return new XMLHttpRequest();
  }
};

ajax({ url: 'http://localhost:5050/posts/1', ...config })
  .pipe(
    mergeMap((res1) =>
      ajax({ url: 'http://localhost:5050/posts/2', body: res1.response, method: 'put', ...config })
        .pipe(mergeMap(res2 => [res2]))
    )
  ).subscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment