The RxJS last operator is similar to first, but works from the other direction. Optionally it accepts a predicate.
RxJS last only emits the last value
When the last
operator is used, the last value emitted by the source should be the only value that is forwarded. The last value emitted by a source is the one before its completion. Hence you need to wait until the source completes. In the example below the last value delivered by the source interval
is 3 and therefore that value is delivered in the result$
Observable after 4 time frames.
const result$ = interval(1).pipe(
take(4),
last(),
);
expectObservable(result$).toBe('----(a|)', { a: 3 });
RxJS last also accepts a predicate
Similar to first
, also the last operator accepts a predicate. Nevertheless also in the predicate case, we have to wait until the final value is delivered, because we never know if there is arriving another value that would satisfy the predicate and therefore change the final output.
const result$ = interval(1).pipe(
take(4),
last(v => v < 2),
);
expectObservable(result$).toBe('----(a|)', { a: 1 });
In above example we’re interested in the last value below 2. The last value below 2 is obviously 1. Also this value is only emitted at completion time.
RxJS last emits an EmptyError if there are no values at all
If there never occurs any next notification in the source, then the output will be an EmptyError
if the last
operator is applied:
const result$ = cold('--|').pipe(
last(),
);
expectObservable(result$).toBe('--#', null, new EmptyError());
RxJS last emits an EmptyError if no values satisfy the predicate
Similar to the example before, there’s also an EmptyError
delivered when the predicate never returns true for the values emitted by the source.
const result$ = interval(1).pipe(
take(6),
last(v => v > 5),
);
expectObservable(result$).toBe('------#', null, new EmptyError());
Importantly, also in this case, the source stream needs to complete. Because before its completion we don’t know about all values emitted.
Exercise for the RxJS last operator 💪
Given are the output values below delivered by the of
operator. Based on that values, only deliver the last truthy value.
of(0, {}, 1, true, null, undefined, '', false)
As always the code examples can be found on GitHub.