fix(docs-infra): fix the backoff example (#38896)
Previously, the `backoff()` example did not work as intended. More specifically, the `range(1, maxTries)` observable would complete immediately after emitting the `maxTries`th value, causing the overall observable to also complete. As a result, it would only make `maxTries - 1` attempts to recover from an error. More importantly, the outer observable would complete successfully instead of erroring. This commit fixes the `backoff()` operator by ensuring it makes exactly `maxTries` attempts to recover and it propagates the actual error to the outer observable. The test for this change is added in the next commit. PR Close #38896
This commit is contained in:
parent
19d543f71e
commit
37297cfed7
|
@ -1,17 +1,18 @@
|
|||
// TODO: Add unit tests for this file.
|
||||
import { pipe, range, timer, zip } from 'rxjs';
|
||||
import { of, pipe, range, throwError, timer, zip } from 'rxjs';
|
||||
import { ajax } from 'rxjs/ajax';
|
||||
import { retryWhen, map, mergeMap } from 'rxjs/operators';
|
||||
import { map, mergeMap, retryWhen } from 'rxjs/operators';
|
||||
|
||||
function backoff(maxTries, ms) {
|
||||
return pipe(
|
||||
retryWhen(attempts => zip(range(1, maxTries), attempts)
|
||||
.pipe(
|
||||
map(([i]) => i * i),
|
||||
mergeMap(i => timer(i * ms))
|
||||
)
|
||||
)
|
||||
);
|
||||
function backoff(maxTries, delay) {
|
||||
return pipe(
|
||||
retryWhen(attempts =>
|
||||
zip(range(1, maxTries + 1), attempts).pipe(
|
||||
mergeMap(([i, err]) => (i > maxTries) ? throwError(err) : of(i)),
|
||||
map(i => i * i),
|
||||
mergeMap(v => timer(v * delay)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
ajax('/api/endpoint')
|
||||
|
|
Loading…
Reference in New Issue