Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const source$ = Rx.Observable.of(1,2,3,4)
- const job$ = source$
- .do(x=>console.log("emitted from source " + x))
- .switchMap(n => fakeAsyncWork(n))
- .do(x=>console.log("emitted from switch " + x))
- .scan((a,c) => a.concat(c), [])
- let fakeAsyncWork = (sec): Rx.Observable<any> => {
- return new Rx.Observable(observer => {
- console.log("start waiting for " + sec);
- setTimeout(() => {
- console.log("finish waiting " + sec);
- observer.next(sec)
- }, sec*1000);
- });
- }
- job$.subscribe(x => console.log(x) );
- "emitted from source 1"
- "start waiting for 1"
- "emitted from source 2"
- "start waiting for 2"
- "emitted from source 3"
- "start waiting for 3"
- "emitted from source 4"
- "start waiting for 4"
- "finish waiting 1"
- "finish waiting 2"
- "finish waiting 3"
- "finish waiting 4"
- "emitted from switch 4"
- [4]
Add Comment
Please, Sign In to add comment