Demo:  skipLast

The skipLast is a filter operator which allows us to skip the last N items from the source.

The operators works by keeping the latest N items in the buffer, so that it can discard them as the source completes.

Beyond the Nth item from source, it keeps emitting the earliest item, as it keeps adding a new item into it's buffer.

skipLast(skipLastNItems)
//Emits 0 to 7
const source = timer(500, 1000).pipe(take(8));

//emits earlier values after keeping the latest 3 values in buffer
const filterItems = skipLast(3);

const output = source.pipe(filterItems).subscribe(v=>console.log(v))
What is happening ?

The skipLast is a filter operator which allows us to skip the last N items from the source.

Let's understand how it works with our demo.

  • The source is supposed to emit 8 items, starting with 0, 1, 2, upto 7.

But, since we are not interested in the last three items from the source:

  • We have piped our source through the "skip Last(3)" filter operator.

This filter does not emit anything untill it has 3 items in it's buffer.

  • Afterwards, it keeps emitting the earliest item from the buffer for every new incoming item.

For instance, after keeping 0, 1 and 2 in it's buffer:

  • It emits 0 when 3 arrives.
  • Again it emits 1 as 4 arrives and so on. At the end, when the source completes, it discards the last 3 items, namely 5, 6 & 7 from it's buffer.