create
signature: create(subscribe: function)
Example 1: Observable that emits multiple values
Example 2: Observable that emits even numbers on timer
import { Observable } from 'rxjs/Observable';
/*
*/
const evenNumbers = Observable.create(function(observer) {
const interval = setInterval(() => {
if (value % 2 === 0) {
observer.next(value);
}
value++;
}, 1000);
});
//output: 0...2...4...6...8
const subscribe = evenNumbers.subscribe(val => console.log(val));
//unsubscribe after 10 seconds
setTimeout(() => {
subscribe.unsubscribe();
}, 10000);
Additional Resources
- create
- Official docs
- Shane Osbourne