diff options
author | Elizabeth Hunt <me@liz.coffee> | 2025-08-18 10:39:18 -0700 |
---|---|---|
committer | Elizabeth Hunt <me@liz.coffee> | 2025-08-18 10:39:45 -0700 |
commit | 08385da331f2c39ecd10ffc47d555b3bf1fb0063 (patch) | |
tree | e7628e99dd371eb541742b40666146b859487f1e | |
parent | 3a303475b763cbbf4d3491146582faccd8b93c99 (diff) | |
download | pengueno-08385da331f2c39ecd10ffc47d555b3bf1fb0063.tar.gz pengueno-08385da331f2c39ecd10ffc47d555b3bf1fb0063.zip |
-rw-r--r-- | lib/trace/metric/trace.ts | 5 | ||||
-rw-r--r-- | lib/types/fn/either.ts | 24 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | 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<MetricsTraceSupplier> { 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<E, T> extends _Tagged implements IEither<E, T> { attempts: number = 3, interval: Mapper<number, Promise<void>> = (attempt) => Either.attemptWait(attempt), ): Promise<IEither<E, T>> { - const res: IEither<T, E> = await Array(attempts) - .fill(0) - .reduce( - async (_result, _i, attempt) => { - await interval(attempt); - const result: IEither<T, E> = await _result; - return result.joinRightAsync( - () => s().then((s) => s.swap()), - (res, _prevError) => res, - ); - }, - Promise.resolve(Either.right<T, E>(<E>new Error('No attempts made'))), + let result: IEither<T, E> = Either.right<T, E>(<E>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<string, string>('attempt 1 failed')) .mockResolvedValueOnce(Either.left<string, string>('attempt 2 failed')) .mockResolvedValueOnce(Either.right<string, string>('attempt 3 success')); |