Skip to content

Function: last()

last<T>(predicate?): Operator<T, T>

Defined in: operators/last.ts:20

Creates a stream operator that emits only the last value from the source stream that matches an optional predicate.

This operator must consume the entire source stream to find the last matching value. It caches the last value that satisfies the predicate (or the last value of the stream if no predicate is provided) and emits it only when the source stream completes.

Type Parameters

T

T = any

The type of the values in the source stream.

Parameters

predicate?

(value) => CallbackReturnType<boolean>

An optional function to test each value. It receives the value and should return true to indicate a match.

Returns

Operator<T, T>

An Operator instance that can be used in a stream's pipe method.

Throws

Throws an error with the message "No elements in sequence" if no matching value is found before the source stream completes.

Examples

From last.spec.ts:4

typescript
const testStream = from([1, 2, 3, 4]);
const lastStream = testStream.pipe(last());
lastStream.subscribe({
  next: (value) => {
    expect(value).toBe(4); // Should emit the last value
    done();
  },
  error: (err) => done.fail(err),
});

Released under the MIT License.