This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Repeat the Execution of an Observable with RxJS repeat

    André StaltzAndré Staltz

    Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson we learn how repeat works.

    rxjsRxJS
    ^5.0.0
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 Retry is a useful operator to repeat the execution of the input observable multiple times, but it only does that if there was an error happen on the input observable. Sometimes it will be useful to create repetition even if there is no error. So let's say that foo would not have the number two, then bar when mapped, would not have an error. Thus the use case for an operator called repeat.

    00:26 The usage of repeat is quite similar to the usage of retry. You can think of it as actually replace the completion notification. This complete with the whole input observable bar itself. If we do that, then it looks like this. Every time we replace that completion it would just keep on going forever because by default repeat will just repeat forever, and it will just keep on doing that. Just like retry it takes an argument here which you can specify the total amount of times that bar will be repeated.

    01:03 So we can say three, that means that after the third time that bar is executed it will just complete. If you run this we see A, B, C, D, and then after the third time it will complete. Repeat can be useful for creating observables from others, for instance that are meant to have some repeated pattern or rhythm.

    Discuss

    Discuss