From fb9a69adc29b6721faaa7f83c14ad90e72c6d3cc Mon Sep 17 00:00:00 2001 From: themodernhakr Date: Wed, 11 Jun 2025 23:51:22 -0500 Subject: [PATCH] Fix AsyncDoEither errors --- package.json | 2 +- src/either.ts | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 20f2f30..2bc54ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fp-lib", - "version": "1.0.1", + "version": "1.0.2", "description": "Zero-dependency functional programming library optimized for Bun", "main": "dist/index.js", "module": "dist/index.js", diff --git a/src/either.ts b/src/either.ts index ce1f28e..3d05cc7 100644 --- a/src/either.ts +++ b/src/either.ts @@ -216,24 +216,28 @@ export class AsyncDoEither, E> { f: (scope: T) => Either | Promise>, ): AsyncDoEither, E> { const newPromise = this.promise.then(async (currentEither) => { - if (isLeft(currentEither)) { - return currentEither as unknown as Either, E> + // Propagate Left case as is + if (currentEither._tag === 'Left') { + return currentEither as Either, E> } const scope = currentEither.right try { const nextEither = await f(scope) - if (isLeft(nextEither)) { - return nextEither as unknown as Either, E> + // Propagate Left from bound computation + if (nextEither._tag === 'Left') { + return nextEither as Either, E> } + // Extend scope with new value return right({ ...scope, [key]: nextEither.right, }) as Either, E> } catch (err) { - return left(err as E) as Either, E> + // Handle unexpected exceptions + return left(err as E) } }) @@ -248,8 +252,8 @@ export class AsyncDoEither, E> { */ return(f: (scope: T) => B): Promise> { return this.promise.then(either => { - if (isLeft(either)) { - return either as unknown as Either + if (either._tag === 'Left') { + return either as Either } return right(f(either.right)) }) @@ -283,15 +287,15 @@ export class AsyncDoEither, E> { | Promise>): AsyncDoEither { const newPromise = this.promise.then(async (currentEither) => { - if (isLeft(currentEither)) { + if (currentEither._tag === 'Left') { return currentEither } const scope = currentEither.right try { const effectEither = await f(scope) - if (isLeft(effectEither)) { - return effectEither as unknown as Either + if (effectEither._tag === 'Left') { + return effectEither as Either } return currentEither } catch (err) {