endWith

function stable

Returns an observable that will emit all values from the source, then synchronously emit the provided value(s) immediately after the source completes.

endWith<T>(...values: (SchedulerLike | T)[]): MonoTypeOperatorFunction<T>

Parameters

values

Items you want the modified Observable to emit last.

Returns

MonoTypeOperatorFunction<T>: A function that returns an Observable that emits all values from the source, then synchronously emits the provided value(s) immediately after the source completes.

Description

NOTE: Passing a last argument of a Scheduler is deprecated, and may result in incorrect types in TypeScript.

This is useful for knowing when an observable ends. Particularly when paired with an operator like takeUntil

endWith marble diagram

Example

Emit values to know when an interval starts and stops. The interval will stop when a user clicks anywhere on the document.

import { interval, fromEvent } from 'rxjs';
import { map, startWith, takeUntil, endWith } from 'rxjs/operators';

const ticker$ = interval(5000).pipe(
  map(() => 'tick'),
);

const documentClicks$ = fromEvent(document, 'click');

ticker$.pipe(
  startWith('interval started'),
  takeUntil(documentClicks$),
  endWith('interval ended by click'),
)
.subscribe(
  x = console.log(x);
)

// Result (assuming a user clicks after 15 seconds)
// "interval started"
// "tick"
// "tick"
// "tick"
// "interval ended by click"

Overloads

endWith(scheduler: SchedulerLike): MonoTypeOperatorFunction<T>

Deprecation Notes

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

Parameters

scheduler

Type: SchedulerLike.

Returns

MonoTypeOperatorFunction<T>

endWith(...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>>

endWith(...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/endWith