diff options
Diffstat (limited to 'lib/types')
-rw-r--r-- | lib/types/fn/either.ts | 24 |
1 files changed, 11 insertions, 13 deletions
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( |