Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active January 16, 2017 04:12
Show Gist options
  • Save ORESoftware/de09ab4b15bd2fbff3b345636f29dcf5 to your computer and use it in GitHub Desktop.
Save ORESoftware/de09ab4b15bd2fbff3b345636f29dcf5 to your computer and use it in GitHub Desktop.
simple observable creation
//import RxJS5
const Rx = require('rxjs');
// create an observable
// An observable is: "just a function that takes a subscriber and returns a function"
const obs = Rx.Observable.create(sub => {
const to = setTimeout(() => {
// do some work
// then fire the following *callbacks*
sub.next();
//if we are one and done, then fire complete()
sub.complete();
}, 200);
return function unsubscribe(){
// if we unsubscribe from this observable before the timeout fires
// this function is invoked, essentially the purpose is for cleanup
clearTimeout(to);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment