最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Is there a way not to return error wrapped by TaskEither? - Stack Overflow

programmeradmin1浏览0评论

Im working with fp-ts library. My function runs in paralel 2 other functions (lets call them first and second) that can either fail or succeed. They both return something like TE.TaskEither<Error, User>

So my function calls them pretty straighforward TE.sequenceArray([first, second])

This function return type is TE.TaskEither<Error, ReadonlyArray<User>>

Now, the behvior is suppoused to be like this:

case errors, succeed:
of [err, err], _ -> left(user does not exist)
of _, [el] -> right(el)
of _, _ -> left(internal server error)

Meaning if any of users is found, the function should return ReadonlyArray<User>> with either 2 or 1 users, and not throw error. If there are 2 errors thrown meaning no user is found it should also return Error wrapped in TaskEither.

Sorry if my explenation is a bit blurry im still pretty new to fp-ts so please be kind to me:)

Ive tried to use TE.fold and TE.chain but the issue is that i can't find the way to ignore error when one of calls return it

I tried:

F.pipe(
  TE.sequenceArray([fist, second]),
  TE.chain(successes => {
     if (successes.length > 0) {
        return TE.right(successes);
    } else {
      return TE.left(Error());
    }
  }),
  TE.map(results => results.flat()),
);

This solution seams to ignore my if statement

发布评论

评论列表(0)

  1. 暂无评论