eliminating-the-subscription-for-an-observable-example3
An example for "Eliminating the Subscription for an Observable in Several Ways" article
☞ https://goo.gl/asCv1B
HTML
<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>
<script src="https://unpkg.com/[email protected]/"></script>
JavaScript
import { interval, fromEvent } from 'rxjs';
import { switchMap, mapTo } from 'rxjs/operators';
//emit every click
const source = fromEvent(document, 'click');
//if another click comes within 3s, message will not be emitted
const example = source.pipe(
switchMap(val => interval(3000).pipe(mapTo('Hello, I made it!')))
);
//(click)...3s...'Hello I made it!'...(click)...2s(click)...
const subscribe = example.subscribe(val => console.log(val));