startWith

function stable

Returns an observable that, at the moment of subscription, will synchronously emit all values provided to this operator, then subscribe to the source and mirror all of its emissions to subscribers.

startWith<T, D>(...values: D[]): OperatorFunction<T, T | D>

Parameters

values

Items you want the modified Observable to emit first.

Returns

OperatorFunction<T, T | D>: A function that returns an Observable that synchronously emits provided values before subscribing to the source Observable.

Description

This is a useful way to know when subscription has occurred on an existing observable.

First emits its arguments in order, and then any emissions from the source.

startWith marble diagram

Examples

Emit a value when a timer starts.

import { timer } from 'rxjs';
import { startWith, map } from 'rxjs/operators';

timer(1000)
  .pipe(
    map(() => 'timer emit'),
    startWith('timer start')
  )
  .subscribe(x => console.log(x));

// results:
// "timer start"
// "timer emit"

Overloads

startWith(value: null): OperatorFunction<T, T | null>

Parameters

value

Type: null.

Returns

OperatorFunction<T, T | null>

startWith(value: undefined): OperatorFunction<T, T | undefined>

Parameters

value

Type: undefined.

Returns

OperatorFunction<T, T | undefined>

startWith(...valuesAndScheduler: [any, SchedulerLike]): OperatorFunction<T, T | ValueFromArray<A>>

Deprecation Notes

The scheduler parameter will be removed in v8. Use scheduled and concatAll. Details: https://rxjs.dev/deprecations/scheduler-argument

Parameters

valuesAndScheduler

Type: [any, SchedulerLike].

Returns

OperatorFunction<T, T | ValueFromArray<A>>

startWith(...values: A): OperatorFunction<T, T | ValueFromArray<A>>

Parameters

values

Type: A.

Returns

OperatorFunction<T, T | ValueFromArray<A>>

See Also

© 2015–2021 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/startWith