Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created July 18, 2017 13:58
Show Gist options
  • Save ajcrites/779f1f2ba6d481425efcb8ab6af50a13 to your computer and use it in GitHub Desktop.
Save ajcrites/779f1f2ba6d481425efcb8ab6af50a13 to your computer and use it in GitHub Desktop.
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mapTo';
// This observable is subscribed to and emits observables
Observable.interval(1000)
.switchMap(() =>
// This inner observable is 'flattened' by `.switchMap` and folded into the outer observable stream
Observable.interval(1000).mapTo('a')
.map(a => {
// This will be logged because it is part of the same observable stream
console.log(a);
// Nothing subscribes to this observable
return Observable.interval(1000).mapTo('b')
.map(b => {
console.log(b);
return 'called';
})
})
// This logs out the observable returned from the inner Observable
).subscribe(obs => console.log(obs));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment