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/types/fn/either.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'lib/types/fn/either.ts') 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( -- cgit v1.2.3-70-g09d2