last

function stable

Returns an Observable that emits only the last item emitted by the source Observable. It optionally takes a predicate function as a parameter, in which case, rather than emitting the last item from the source Observable, the resulting Observable will emit the last item from the source Observable that satisfies the predicate.

last<T, D>(predicate?: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: D): OperatorFunction<T, T | D>

Parameters

predicate

Optional. Default is undefined.

The condition any source emitted item has to satisfy.

defaultValue

Optional. Default is undefined.

An optional default value to provide if last predicate isn't met or no values were emitted.

Returns

OperatorFunction<T, T | D>: A function that returns an Observable that emits only the last item satisfying the given condition from the source, or a NoSuchElementException if no such items are emitted.

Throws

EmptyError Delivers an EmptyError to the Observer's error callback if the Observable completes before any next notification was sent.

Error - Throws if no items that match the predicate are emitted by the source Observable.

Description

last marble diagram

It will throw an error if the source completes without notification or one that matches the predicate. It returns the last value or if a predicate is provided last value that matches the predicate. It returns the given default value if no notification is emitted or matches the predicate.

Example

Last alphabet from the sequence.

import { from } from 'rxjs';
import { last } from 'rxjs/operators';

const source = from(['x', 'y', 'z']);
const example = source.pipe(last());
//output: "Last alphabet: z"
example.subscribe(val => console.log(`Last alphabet: ${val}`));

Default value when the value in the predicate is not matched.

import { from } from 'rxjs';
import { last } from 'rxjs/operators';

const source = from(['x', 'y', 'z']);
const example = source.pipe(last(char => char === 'a','not exist'));
//output: "'a' is not exist."
example.subscribe(val => console.log(`'a' is ${val}.`));

Overloads

last(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>

Parameters

predicate

Type: BooleanConstructor.

Returns

OperatorFunction<T, TruthyTypesOf<T>>

last(predicate: BooleanConstructor, defaultValue: D): OperatorFunction<T, TruthyTypesOf<T> | D>

Parameters

predicate

Type: BooleanConstructor.

defaultValue

Type: D.

Returns

OperatorFunction<T, TruthyTypesOf<T> | D>

last(predicate?: null, defaultValue?: D): OperatorFunction<T, T | D>

Parameters

predicate

Optional. Default is undefined.

Type: null.

defaultValue

Optional. Default is undefined.

Type: D.

Returns

OperatorFunction<T, T | D>

last(predicate: (value: T, index: number, source: Observable<T>) => value is S, defaultValue?: S): OperatorFunction<T, S>

Parameters

predicate

Type: (value: T, index: number, source: Observable) => value is S.

defaultValue

Optional. Default is undefined.

Type: S.

Returns

OperatorFunction<T, S>

last(predicate: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: D): OperatorFunction<T, T | D>

Parameters

predicate

Type: (value: T, index: number, source: Observable) => boolean.

defaultValue

Optional. Default is undefined.

Type: D.

Returns

OperatorFunction<T, T | D>

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