From 58f2abef012ec738844977a7529792be43d8deb6 Mon Sep 17 00:00:00 2001 From: Sonu Kapoor Date: Sat, 19 Sep 2020 11:52:03 -0400 Subject: [PATCH] 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 --- .../practical-observable-usage/src/backoff.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/aio/content/examples/practical-observable-usage/src/backoff.ts b/aio/content/examples/practical-observable-usage/src/backoff.ts index 70490b917d..677ed0b2a4 100644 --- a/aio/content/examples/practical-observable-usage/src/backoff.ts +++ b/aio/content/examples/practical-observable-usage/src/backoff.ts @@ -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')