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 waits for the source to complete before emitting. If no value is found and no default is provided, it emits an EmptyError.
The condition any source emitted item has to satisfy.If not provided, last emits the very last value from the source.If provided, last emits the last value that returns true from this function.
OperatorFunction<T, T | D> - A function that returns an Observable that emits only the last item satisfying the given condition from the source, or an EmptyError if no such items are emitted.
import { from, last } from 'rxjs';const source = from(['x', 'y', 'z']);const result = source.pipe(last());result.subscribe(value => console.log(`Last alphabet: ${value}`));// Output:// Last alphabet: z
import { from, last } from 'rxjs';const source = from(['x', 'y', 'z']);const result = source.pipe( last(char => char === 'a', 'not found'));result.subscribe(value => console.log(`'a' is ${value}.`));// Output: 'a' is not found.
Copy
Ask AI
import { interval, last, take, switchMap } from 'rxjs';import { ajax } from 'rxjs/ajax';// Poll API 5 times, get the last responseconst polling$ = interval(1000).pipe( take(5), switchMap(() => ajax.getJSON('/api/status')), last());polling$.subscribe(status => { console.log('Final status:', status);});
import { interval, last, take, timeout } from 'rxjs';const values$ = interval(100).pipe( take(10), timeout(550), // May not complete all 10 last(null, -1) // Default to -1 if timeout);values$.subscribe( value => console.log('Last value:', value), err => console.error('Timeout'));
import { EMPTY, last, of } from 'rxjs';// EmptyError when no value emittedEMPTY.pipe(last()).subscribe( value => console.log('Value:', value), err => console.error('Error:', err.name));// Output: Error: EmptyError// No error with default valueEMPTY.pipe(last(null, 'default')).subscribe( value => console.log('Value:', value));// Output: Value: default// EmptyError when condition not metof(1, 2, 3).pipe( last(x => x > 10)).subscribe( value => console.log('Value:', value), err => console.error('Error:', err.name));// Output: Error: EmptyError
import { of, last, first, takeLast } from 'rxjs';const nums$ = of(1, 2, 3, 4, 5);// Get last single valuenums$.pipe(last()).subscribe(console.log);// Output: 5// Get first single valuenums$.pipe(first()).subscribe(console.log);// Output: 1// Get last 3 values (array)nums$.pipe(takeLast(3)).subscribe(console.log);// Output: [3, 4, 5]
last must wait for the source to complete before emitting. For long-running or infinite streams, this operator will never emit.
Copy
Ask AI
import { interval, last } from 'rxjs';// This will NEVER emit (infinite stream)const infinite$ = interval(1000).pipe( last());// Add take() to make it finiteconst finite$ = interval(1000).pipe( take(5), last());finite$.subscribe(value => { console.log('Last value:', value);});// Output after 5 seconds: Last value: 4