Last active
November 7, 2017 16:56
-
-
Save codeachange/18fc13f321caffcff34e5e6a6b0394cb to your computer and use it in GitHub Desktop.
observable in plain javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// =========== 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