W3cubDocs

/RxJS

takeLast

function stable

Emits only the last count values emitted by the source Observable.

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>: An Observable that emits at most the last count values emitted by the source Observable.

Throws

ArgumentOutOfRangeError When using takeLast(i), it delivers an ArgumentOutOrRangeError to the Observer's error callback if i < 0.

Description

Remembers the latest count values, then emits those only when the source completes.

takeLast marble diagram

takeLast returns an Observable that emits at most the last count values emitted by the source Observable. If the source emits fewer than count values then all of its values are emitted. This operator must wait until the complete notification emission from the source in order to emit the next values on the output Observable, because otherwise it is impossible to know whether or not more values will be emitted on the source. For this reason, all values are emitted synchronously, followed by the complete notification.

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–2018 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