startWith
signature: startWith(an: Values): Observable
startWith(an: Values): ObservableEmit given value first.
๐ก A BehaviorSubject can also start with an initial value!
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 )
Related Recipes
Additional Resources
startWith ๐ฐ - Official docs
Displaying initial data with startWith ๐ฅ ๐ต - John Linquist
Clear data while loading with startWith ๐ฅ ๐ต - Andrรฉ Staltz
Combination operator: concat, startWith ๐ฅ ๐ต - Andrรฉ Staltz
๐ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/startWith.ts
Last updated