Created
June 8, 2017 05:24
-
-
Save shallwefootball/ebc1829454699d9941ed80935169edeb 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
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