This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Transformation operator: repeat

1:28 RxJS lesson by

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

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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

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.

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.

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.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?