( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { Observable } from 'rxjs';
/*
Create an observable that emits 'Hello' and 'World' on
subscription.
*/
const hello = Observable.create(function(observer) {
observer.next('Hello');
observer.next('World');
observer.complete();
});
//output: 'Hello'...'World'
const subscribe = hello.subscribe(val => console.log(val));
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { Observable } from 'rxjs';
/*
Increment value every 1s, emit even numbers.
*/
const evenNumbers = Observable.create(function(observer) {
let value = 0;
const interval = setInterval(() => {
if (value % 2 === 0) {
observer.next(value);
}
value++;
}, 1000);
return () => clearInterval(interval);
});
//output: 0...2...4...6...8
const subscribe = evenNumbers.subscribe(val => console.log(val));
//unsubscribe after 10 seconds
setTimeout(() => {
subscribe.unsubscribe();
}, 10000);
- create 📰 - Official docs
- Creation operators: Create() 🎥 💵 - André Staltz
- Using Observable.create for fine-grained control 🎥 💵 - Shane Osbourne
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/GenerateObservable.ts