Created
April 9, 2018 21:46
-
-
Save Christopher2K/cedf4fc0185488f8d03b08f4fda24a9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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