startWith

signature: startWith(an: Values): Observable

Emit given value first.


๐Ÿ’ก A BehaviorSubject can also start with an initial value!


Ultimate RxJS

Examples

( example tests )

Example 1: startWith on number sequence

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { startWith } from 'rxjs/operators';
import { of } from 'rxjs';

//emit (1,2,3)
const source = of(1, 2, 3);
//start with 0
const example = source.pipe(startWith(0));
//output: 0,1,2,3
const subscribe = example.subscribe(val => console.log(val));

Example 2: startWith for initial scan value

( StackBlitz | | jsBin | jsFiddle )

Example 3: startWith multiple values

( StackBlitz | jsBin | jsFiddle )

Additional Resources


๐Ÿ“ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/startWith.ts

Last updated