Skip to content

Commit d57adb7

Browse files
committed
3.3 Fixed retryWhen example
1 parent 055dc2c commit d57adb7

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

Part 3 - Taming the sequence/3. Advanced error handling.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public final Observable<T> retryWhen(Func1<? super Observable<? extends java.lan
136136

137137
The argument to `retryWhen` is a function that takes an observable and returns another. The input observable emits all the errors that `retryWhen` encounters. The resulting observable signals when to retry:
138138
* if it emits a value, `retryWhen` will retry,
139-
* if it terminates, `retryWhen` will emit the error and not retry.
139+
* if it terminates with error, `retryWhen` will emit the error and not retry.
140+
* if it terminates successfully, `retryWhen` will terminate successfully
140141
Note that the type of the signaling obserable of the actual values emitted don't matter. The values are discarded and the observable is only used for timing.
141142

142143
In the next example, we will construct a retrying policy where we wait 100ms before retrying.
@@ -147,22 +148,28 @@ Observable<Integer> source = Observable.create(o -> {
147148
o.onNext(2);
148149
o.onError(new Exception("Failed"));
149150
});
150-
151-
source.retryWhen((o) -> o.take(2).delay(100, TimeUnit.MILLISECONDS))
151+
152+
source.retryWhen((o) -> o
153+
.take(2)
154+
.delay(100, TimeUnit.MILLISECONDS)
155+
.concatWith(Observable.error(new Exception("Done"))))
152156
.timeInterval()
153-
.subscribe(System.out::println);
157+
.subscribe(
158+
System.out::println,
159+
System.out::println);
154160
```
155161
Output
156162
```
157-
TimeInterval [intervalInMilliseconds=17, value=1]
163+
TimeInterval [intervalInMilliseconds=21, value=1]
158164
TimeInterval [intervalInMilliseconds=0, value=2]
159-
TimeInterval [intervalInMilliseconds=102, value=1]
165+
TimeInterval [intervalInMilliseconds=104, value=1]
160166
TimeInterval [intervalInMilliseconds=0, value=2]
161-
TimeInterval [intervalInMilliseconds=102, value=1]
167+
TimeInterval [intervalInMilliseconds=103, value=1]
162168
TimeInterval [intervalInMilliseconds=0, value=2]
169+
java.lang.Exception: Done
163170
```
164171

165-
Our source observable emits 2 values and immediately fails. When that happens, the observable of failures inside `retryWhen` emits the error. We delay that emission by 100ms and send it back to signal to retry. `take(2)` guarantees that our signaling observabe will terminate after we receive two errors. That means that `retryWhen` won't retry after 2 failures.
172+
Our source observable emits 2 values and immediately fails. When that happens, the observable of failures inside `retryWhen` emits the error. We delay that emission by 100ms and send it back to signal a retry. `take(2)` guarantees that our signaling observabe will terminate after we receive two errors. That means that `retryWhen` won't retry after 2 failures. We concatenate an error to make `retryWhen` also fail.
166173

167174
## using
168175

0 commit comments

Comments
 (0)