reactive.how 2.0-alpha Menu

takeLast vs take

First, learn about take and stream completion.

Use a ▬ number on these cards, such as 3, to set a maximum amount of events to emit. ❚ take and ❚ takeLast share identical behavior:

  • they both return a new stream of at most amount values.
  • if the input stream source emits fewer than amount values then all of its values are emitted on the output stream.

The stream returned by ❚ take emits the values at the same time as their counterpart on the input stream. This is not the case for ❚ takeLast. Remember: a stream is a sequence of events over time. And we can’t see the future! takeLast must wait for the ◉ complete notification. Only then, we know what are the last values.

takeLast

❚ takeLast returns a new stream of at most amount values:

  • When the input stream completes, the output stream:
    • emits each of the last amount values emitted by the input stream
    • and immediately completes
  • If the input stream never completes, the output stream never emits

Note: the last values are delivered as several separate event values.

See also


take and complete


take vs last


Pipeable operators - Build your own with RxJS!


The JavaScript pipeline operator proposal

💌 I create something new each week!
Learn Reactive Programming and stay tuned.

Occasional updates, plus:

Cédric Soulas - Freelance Developer Advocate. Learn more.

Follow 👨‍💻 Hire me

News

@CedricSoulas