Skip to content

Instantly share code, notes, and snippets.

@codeachange
Last active November 7, 2017 16:56
Show Gist options
  • Save codeachange/18fc13f321caffcff34e5e6a6b0394cb to your computer and use it in GitHub Desktop.
Save codeachange/18fc13f321caffcff34e5e6a6b0394cb to your computer and use it in GitHub Desktop.
observable in plain javascript
// =========== observable ================
var observable = Rx.Observable.create(function subscribe(observer) {
// Keep track of the interval resource
var intervalID = setInterval(() => {
observer.next('hi');
}, 1000);
// Provide a way of canceling and disposing the interval resource
return function unsubscribe() {
clearInterval(intervalID);
};
});
var subscription = observable.subscribe(x => console.log(x));
// Later:
subscription.unsubscribe();
// =========== equivalent plain js ==================
function subscribe(observer) {
var intervalID = setInterval(() => {
observer.next('hi');
}, 1000);
return function unsubscribe() {
clearInterval(intervalID);
};
}
var unsubscribe = subscribe({next: (x) => console.log(x)});
// Later:
unsubscribe(); // dispose the resources
# http://reactivex.io/rxjs/manual/overview.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment