Skip to content

Instantly share code, notes, and snippets.

@Christopher2K
Created April 9, 2018 21:46
Show Gist options
  • Save Christopher2K/cedf4fc0185488f8d03b08f4fda24a9d to your computer and use it in GitHub Desktop.
Save Christopher2K/cedf4fc0185488f8d03b08f4fda24a9d to your computer and use it in GitHub Desktop.
// with [email protected] & [email protected]
import { combineLatest } from 'rxjs/observable/combineLatest';
import { ajax } from 'rxjs/observable/dom/ajax';
import { tap, map } from 'rxjs/operators';
const BASE_URL = 'http://myurl.com';
// Retour un observable AJAX formé par un argument
function loginUserAccordingToData({ someDataThis }) {
return ajax.post(`${BASE_URL}/data`, { someDataThis })
.pipe(
map((response) => {
const { data } = response;
// do something....
return data;
})
);
}
// Retour aussi un observable AJAX formé par un argument
function getUserProfileAccordingToData({ someDataThat }) {
return ajax.getJSON(`${BASE_URL}/profile?data=${someDataThat}}`);
}
// On va succéder et combiner les observables et donc les requêtes
const myBigRequest = ajax.getJSON(`${BASE_URL}/data`)
.pipe(
switchMap(
(data) => combineLatest(loginUserAccordingToData(data), getUserProfileAccordingToData())
)
);
// On exécute le tout
myBigRequest.subscribe(
([loginData, profileData]) => {
alert('SUCCESS !!');
console.log(loginData, profileData);
},
(err) => {
alert('ERROR !!');
console.error(err)
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment