Skip to content

toEither method #13

@rpominov

Description

@rpominov

It would turn Task<S, F> into Task<{success: S} | {failure: F}, any>*.

* Would be cool to use empty instead of any here, but Flow doesn't support it yet: facebook/flow#2225

I've already run into two cases where it could be useful:

In Task.do.

Task.do(function* () {
  const {success, failure} = yield doStuff().toEither()
  // ...
})

In combination with toPromise (task.toEither().toPromise()) because Promise's error path isn't really good to use for expected failures, it's better to reserve it only for uncaught exceptions / bugs (update: or we probably should do it automatically in toPromise #4 (comment)).

The name

I'm not a fun of toEither name, would appreciate a help here. Other names I've considered so far:

  • join — it's better to reserve this name for monadic join (chain(x => x))
  • reduce — conflicts with Fantasy Land's Foldable

Also both join and reduce not very intuitive i.e., bad names either way.

The idea of toEither is that {success: S} | {failure: F} basically poor man's Either so in other words we transform Task<S, F> into Task<Either<S, F>, empty>. The problems are: 1) this explanation is required to understand the name, 2) strictly speaking the name should be toTaksOfEither.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions