Fix AsyncDoEither errors

This commit is contained in:
themodernhakr 2025-06-11 23:51:22 -05:00
parent cb6c80c12e
commit fb9a69adc2
2 changed files with 15 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "fp-lib", "name": "fp-lib",
"version": "1.0.1", "version": "1.0.2",
"description": "Zero-dependency functional programming library optimized for Bun", "description": "Zero-dependency functional programming library optimized for Bun",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.js", "module": "dist/index.js",

View File

@ -216,24 +216,28 @@ export class AsyncDoEither<T extends Record<string, unknown>, E> {
f: (scope: T) => Either<A, E> | Promise<Either<A, E>>, f: (scope: T) => Either<A, E> | Promise<Either<A, E>>,
): AsyncDoEither<T & Record<K, A>, E> { ): AsyncDoEither<T & Record<K, A>, E> {
const newPromise = this.promise.then(async (currentEither) => { const newPromise = this.promise.then(async (currentEither) => {
if (isLeft(currentEither)) { // Propagate Left case as is
return currentEither as unknown as Either<T & Record<K, A>, E> if (currentEither._tag === 'Left') {
return currentEither as Either<T & Record<K, A>, E>
} }
const scope = currentEither.right const scope = currentEither.right
try { try {
const nextEither = await f(scope) const nextEither = await f(scope)
if (isLeft(nextEither)) { // Propagate Left from bound computation
return nextEither as unknown as Either<T & Record<K, A>, E> if (nextEither._tag === 'Left') {
return nextEither as Either<T & Record<K, A>, E>
} }
// Extend scope with new value
return right({ return right({
...scope, ...scope,
[key]: nextEither.right, [key]: nextEither.right,
}) as Either<T & Record<K, A>, E> }) as Either<T & Record<K, A>, E>
} catch (err) { } catch (err) {
return left(err as E) as Either<T & Record<K, A>, E> // Handle unexpected exceptions
return left(err as E)
} }
}) })
@ -248,8 +252,8 @@ export class AsyncDoEither<T extends Record<string, unknown>, E> {
*/ */
return<B>(f: (scope: T) => B): Promise<Either<B, E>> { return<B>(f: (scope: T) => B): Promise<Either<B, E>> {
return this.promise.then(either => { return this.promise.then(either => {
if (isLeft(either)) { if (either._tag === 'Left') {
return either as unknown as Either<B, E> return either as Either<B, E>
} }
return right(f(either.right)) return right(f(either.right))
}) })
@ -283,15 +287,15 @@ export class AsyncDoEither<T extends Record<string, unknown>, E> {
| Promise<Either<unknown, E>>): AsyncDoEither<T, E> | Promise<Either<unknown, E>>): AsyncDoEither<T, E>
{ {
const newPromise = this.promise.then(async (currentEither) => { const newPromise = this.promise.then(async (currentEither) => {
if (isLeft(currentEither)) { if (currentEither._tag === 'Left') {
return currentEither return currentEither
} }
const scope = currentEither.right const scope = currentEither.right
try { try {
const effectEither = await f(scope) const effectEither = await f(scope)
if (isLeft(effectEither)) { if (effectEither._tag === 'Left') {
return effectEither as unknown as Either<T, E> return effectEither as Either<T, E>
} }
return currentEither return currentEither
} catch (err) { } catch (err) {