RxJS - Rewriting setInterval using observable

A small post on how we can rewrite setInterval using observable.

setInterval Example:

js
1const intervalId = setInterval(() => console.log("Running setInterval"), 100); setTimeout(() => clearInterval(intervalId), 1000);

setInterval example using observable:

js
1const {interval} = require('rxjs'); const intervalObservable = interval(100); const subscription = intervalObservable.subscribe(() => console.log("Running setInterval")); setTimeout(() => subscription.unsubscribe(), 1000);