Skip to content

Instantly share code, notes, and snippets.

@shallwefootball
Created June 8, 2017 05:24
Show Gist options
  • Save shallwefootball/ebc1829454699d9941ed80935169edeb to your computer and use it in GitHub Desktop.
Save shallwefootball/ebc1829454699d9941ed80935169edeb to your computer and use it in GitHub Desktop.
const switchBtnEl = document.getElementById('switchBtn');
const switchBtn$ = Rx.Observable.fromEvent(switchBtnEl, 'click');
const mergeEl = document.getElementById('mergeBtn');
const mergeBtn$ = Rx.Observable.fromEvent(mergeEl, 'click');
const goFetchFn = () => {
return Rx.Observable.fromPromise(
fetch('http://localhost:3000/gogo').then(res => {
console.log('goFetchFn');
return res.json();
})
);
}
const comeFetchFn = () => {
return Rx.Observable.fromPromise(
fetch('http://localhost:3000/come').then(res => {
console.log('comeFetchFn')
return res.json();
})
);
}
const clickSwitchGo$ = switchBtn$.switchMap(() => {
console.log('click!! switch~~');
return goFetchFn();
});
const clickMergeCome$ = mergeBtn$.mergeMap(() => {
console.log('click!! merge~~');
return comeFetchFn();
})
clickSwitchGo$.subscribe(data => {
console.log('subscribe switch!!', data);
});
clickMergeCome$.subscribe(data => {
console.log('subscribe merge!!', data);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment