create

signature: create(subscribe: function)

Create an observable with given subscription function.

Ultimate RxJS

Examples

Example 1: Observable that emits multiple values

( 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));

Example 2: Observable that emits even numbers on timer

( StackBlitz | jsBin | jsFiddle )

Additional Resources


📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/GenerateObservable.ts

Last updated