DEV Community

Discussion on: Practical Guide to Fp-ts: P3 — Task, Either, TaskEither

Collapse
 
seyfer profile image
Oleg Abrazhaev

hey, great articles cycle. waiting for the next one!

after you got T.Task<Error | Resp> from TaskEither how would you take the value out?
with the use of one of the destructors like getOrElse or any better way?
I mean I want to get Error or Resp out of a Task<> as a value.

Collapse
 
ryanleecode profile image
Ryan Lee • Edited

Task is just a promise, so you can just await it and use an if statement.

import * as T from 'fp-ts/lib/Task'

type Resp = { code: number; description: string }

declare const task: T.Task<Error | Resp>
(async() => {
  const result = await task()
  if (result instanceof Error) {
    // Result is Error
  } else {
    // Result is Resp
  }
})()
Collapse
 
ryanleecode profile image
Ryan Lee

That section of the post didn't make of a lot of sense. I've rewritten it so the final type is T.Task<Resp>, since an error should never occur.

Collapse
 
seyfer profile image
Oleg Abrazhaev

I think adding absurd, constVoid, unsafeCoerce without explanation only made it harder to understand for beginners.