From 08385da331f2c39ecd10ffc47d555b3bf1fb0063 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Mon, 18 Aug 2025 10:39:18 -0700 Subject: Make retry simpler --- lib/trace/metric/trace.ts | 5 +---- lib/types/fn/either.ts | 24 +++++++++++------------- package.json | 2 +- tst/either.test.ts | 3 ++- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/lib/trace/metric/trace.ts b/lib/trace/metric/trace.ts index 6508831..b28d828 100644 --- a/lib/trace/metric/trace.ts +++ b/lib/trace/metric/trace.ts @@ -49,10 +49,7 @@ export class MetricsTrace implements ITrace { const parentBasedValues = parentBasedMetrics.flatMap((metric) => { const parentStart = this.activeTraces.get(metric.parent!.name)!; - return [ - metric.count.withValue(1.0), - metric.time.withValue(now - parentStart), - ]; + return [metric.count.withValue(1.0), metric.time.withValue(now - parentStart)]; }); const allMetricsToEmit = [...valuesToEmit, ...endedMetricValues, ...parentBasedValues]; diff --git a/lib/types/fn/either.ts b/lib/types/fn/either.ts index ffe06d4..3c39548 100644 --- a/lib/types/fn/either.ts +++ b/lib/types/fn/either.ts @@ -152,20 +152,18 @@ export class Either extends _Tagged implements IEither { attempts: number = 3, interval: Mapper> = (attempt) => Either.attemptWait(attempt), ): Promise> { - const res: IEither = await Array(attempts) - .fill(0) - .reduce( - async (_result, _i, attempt) => { - await interval(attempt); - const result: IEither = await _result; - return result.joinRightAsync( - () => s().then((s) => s.swap()), - (res, _prevError) => res, - ); - }, - Promise.resolve(Either.right(new Error('No attempts made'))), + let result: IEither = Either.right(new Error('No attempts made')); + + for (let attempt = 0; attempt < attempts && result.right().present(); attempt++) { + await interval(attempt); + const currentAttempt = await s().then((s) => s.swap()); + result = await result.joinRightAsync( + () => Promise.resolve(currentAttempt), + (res, _prevError) => res, ); - return res.swap(); + } + + return result.swap(); } static attemptWait( diff --git a/package.json b/package.json index 7a6465d..b49f1e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@emprespresso/pengueno", - "version": "0.0.12", + "version": "0.0.13", "description": "emprespresso pengueno utils", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/tst/either.test.ts b/tst/either.test.ts index 790d392..d87bcab 100644 --- a/tst/either.test.ts +++ b/tst/either.test.ts @@ -26,7 +26,8 @@ describe('Either.retrying', () => { }); test('attempts correct number of times and calls interval with backoff', async () => { - const supplier = jest.fn() + const supplier = jest + .fn() .mockResolvedValueOnce(Either.left('attempt 1 failed')) .mockResolvedValueOnce(Either.left('attempt 2 failed')) .mockResolvedValueOnce(Either.right('attempt 3 success')); -- cgit v1.2.3-70-g09d2