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",
"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",

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>>,
): AsyncDoEither<T & Record<K, A>, E> {
const newPromise = this.promise.then(async (currentEither) => {
if (isLeft(currentEither)) {
return currentEither as unknown as Either<T & Record<K, A>, E>
// Propagate Left case as is
if (currentEither._tag === 'Left') {
return currentEither as Either<T & Record<K, A>, E>
}
const scope = currentEither.right
try {
const nextEither = await f(scope)
if (isLeft(nextEither)) {
return nextEither as unknown as Either<T & Record<K, A>, E>
// Propagate Left from bound computation
if (nextEither._tag === 'Left') {
return nextEither as Either<T & Record<K, A>, E>
}
// Extend scope with new value
return right({
...scope,
[key]: nextEither.right,
}) as Either<T & Record<K, A>, E>
} 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 this.promise.then(either => {
if (isLeft(either)) {
return either as unknown as Either<B, E>
if (either._tag === 'Left') {
return either as Either<B, E>
}
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>
{
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<T, E>
if (effectEither._tag === 'Left') {
return effectEither as Either<T, E>
}
return currentEither
} catch (err) {