Essentials

Skipping

Reducing

Concatenating

  • startWith (soon)
  • endWith (soon)
  • concat (soon)

Combining

Animated example
How it works

take

accepts 1 input stream, an amount

  • When the input stream emits its n-th value:
    • if n < amount, the value is emitted
    • if n == amount, the value is emitted and the stream immediately completes

returns a new stream of at most amount values

last

accepts 1 input stream, a predicate (optional)

  • When the input stream completes, the output stream:
    • emits the last value emitted by the input stream
    • and immediately completes

returns a new stream of at most one value

Published on Monday, 20 Nov. 2017
Revised on Monday, 8 Oct. 2018

As soon as the input stream emits its last value, the output stream emits it? Naaah! How could one be certain that no value comes after? Reactive programming can’t see the future.

We have to wait for a ◉ complete notification (read Take 3 and complete to learn more about it). Only when the stream has completed, we know this value was the last one – and this completion may never happen!

In the animated comparison above, ❚ take returns a stream with a ◉ complete notification overlapped by the 3 value, as it completes immediately. I would like to emphasize that this notification is not intended to deliver the last value of the stream. It is a separate event, without a value (though it can happen immediately after an event value). ❚ last and ❚ take work with an input stream that completes a few seconds after the last value emitted.

Notes

  • What happens if the stream completes without emitting a value depends on the reactive library you use.
  • In RxJS, last accepts an optional predicate. Learn more ➜

Summary

  • A stream can complete emitting a notification (read Take 3 and complete)
  • The ◉ complete notification is a special event with no value
  • A listener can subscribe to a stream and act upon its completion
  • Also, the behavior of some cards like ❚ last is based on this notification

See also


takeLast vs take

Cédric Soulas

Follow Contact Hire me

Freelance Developer Advocate. Motion graphics with code. JavaScript and Elm. cedricsoulas.com

Receive my latest news, tips and animations:

Learn Reactive Programming and stay up-to-date:

News

@CedricSoulas