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

takeWhile

accepts 1 input stream, a predicate function

  • When the input stream emits a value, it is given to the predicate:
    • If the predicate returns ✔ true, the value is emitted
    • otherwise, the value is ignored and the stream immediately completes

returns a new stream of filtered values

Published on Monday, 8 Jan. 2018
Revised on Monday, 8 Oct. 2018

Exercise

In the animation above, can you spot the difference between the outputs? Can you explain why?

Answer: the ◉ complete notification happens at a different moment:

  • On the one hand, ❚ take returns a stream that emits at most amount values and can complete as soon as this amount is reached.
  • On the other hand, ❚ takeWhile has to wait for the predicate to return ✘ false in order to complete.

See also


Can you fill in the blanks?

Cédric Soulas Follow Hire me

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

Subscribe to reactive.how newsletter

Join the Newsletter

Learn Reactive Programming and stay up-to-date:

Receive my latest news, product updates and programming visualizations. You can unsubscribe at any time.

Highlights

@CedricSoulas

Making an illustrated book!

The Illustrated Book of RxJS

Learn more →