takeLast

function stable

Waits for the source to complete, then emits the last N values from the source, as specified by the count argument.

takeLast<T>(count: number): MonoTypeOperatorFunction<T>

Parameters

count

The maximum number of values to emit from the end of the sequence of values emitted by the source Observable.

Returns

MonoTypeOperatorFunction<T>: A function that returns an Observable that emits at most the last count values emitted by the source Observable.

Description

takeLast marble diagram

takeLast results in an observable that will hold values up to count values in memory, until the source completes. It then pushes all values in memory to the consumer, in the order they were received from the source, then notifies the consumer that it is complete.

If for some reason the source completes before the count supplied to takeLast is reached, all values received until that point are emitted, and then completion is notified.

Warning: Using takeLast with an observable that never completes will result in an observable that never emits a value.

Example

Take the last 3 values of an Observable with many values

import { range } from 'rxjs';
import { takeLast } from 'rxjs/operators';

const many = range(1, 100);
const lastThree = many.pipe(takeLast(3));
lastThree.subscribe(x => console.log(x));

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/takeLast