Overview

TaskEither<L, A> represents an asynchronous computation that either yields a value of type A or fails yielding an error of type L. If you want to represent an asynchronous computation that never fails, please see Task.


Table of contents


URI (type alias)

Signature

export type URI = typeof URI

TaskEither (class)

Signature

export class TaskEither<L, A> {
  constructor(readonly value: Task<Either<L, A>>) { ... }
  ...
}

Added in v1.0.0

run (method)

Runs the inner Task

Signature

run(): Promise<Either<L, A>> { ... }

map (method)

Signature

map<B>(f: (a: A) => B): TaskEither<L, B> { ... }

ap (method)

Signature

ap<B>(fab: TaskEither<L, (a: A) => B>): TaskEither<L, B> { ... }

ap_ (method)

Flipped version of ap

Signature

ap_<B, C>(this: TaskEither<L, (b: B) => C>, fb: TaskEither<L, B>): TaskEither<L, C> { ... }

applyFirst (method)

Combine two (parallel) effectful actions, keeping only the result of the first

Signature

applyFirst<B>(fb: TaskEither<L, B>): TaskEither<L, A> { ... }

Added in v1.6.0

applySecond (method)

Combine two (parallel) effectful actions, keeping only the result of the second

Signature

applySecond<B>(fb: TaskEither<L, B>): TaskEither<L, B> { ... }

Added in v1.5.0

chainFirst (method)

Combine two (sequential) effectful actions, keeping only the result of the first

Signature

chainFirst<B>(fb: TaskEither<L, B>): TaskEither<L, A> { ... }

Added in v1.12.0

chainSecond (method)

Combine two (sequential) effectful actions, keeping only the result of the second

Signature

chainSecond<B>(fb: TaskEither<L, B>): TaskEither<L, B> { ... }

Added in v1.12.0

chain (method)

Signature

chain<B>(f: (a: A) => TaskEither<L, B>): TaskEither<L, B> { ... }

fold (method)

Signature

fold<R>(onLeft: (l: L) => R, onRight: (a: A) => R): Task<R> { ... }

foldTask (method)

Similar to fold, but the result is flattened.

Signature

foldTask<R>(onLeft: (l: L) => Task<R>, onRight: (a: A) => Task<R>): Task<R> { ... }

Added in v1.10.0

foldTaskEither (method)

Similar to fold, but the result is flattened.

Signature

foldTaskEither<M, B>(onLeft: (l: L) => TaskEither<M, B>, onRight: (a: A) => TaskEither<M, B>): TaskEither<M, B> { ... }

Added in v1.10.0

mapLeft (method)

Signature

mapLeft<M>(f: (l: L) => M): TaskEither<M, A> { ... }

orElse (method)

Transforms the failure value of the TaskEither into a new TaskEither

Signature

orElse<M>(f: (l: L) => TaskEither<M, A>): TaskEither<M, A> { ... }

alt (method)

Signature

alt(fy: TaskEither<L, A>): TaskEither<L, A> { ... }

Added in v1.6.0

bimap (method)

Signature

bimap<V, B>(f: (l: L) => V, g: (a: A) => B): TaskEither<V, B> { ... }

Added in v1.2.0

attempt (method)

Return Right if the given action succeeds, Left if it throws

Signature

attempt<M = L>(): TaskEither<M, Either<L, A>> { ... }

Added in v1.10.0

filterOrElse (method)

Signature

filterOrElse<B extends A>(p: Refinement<A, B>, zero: L): TaskEither<L, B>
filterOrElse(p: Predicate<A>, zero: L): TaskEither<L, A> { ... }

Added in v1.11.0

filterOrElseL (method)

Signature

filterOrElseL<B extends A>(p: Refinement<A, B>, zero: (a: A) => L): TaskEither<L, B>
filterOrElseL(p: Predicate<A>, zero: (a: A) => L): TaskEither<L, A> { ... }

Added in v1.11.0

URI (constant)

Signature

export const URI = ...

taskEither (constant)

Signature

export const taskEither: Monad2<URI> & Bifunctor2<URI> & Alt2<URI> & MonadIO2<URI> & MonadTask2<URI> = ...

Added in v1.0.0

taskEitherSeq (constant)

Like TaskEither but ap is sequential

Signature

export const taskEitherSeq: typeof taskEither = ...

Added in v1.10.0

bracket (function)

Make sure that a resource is cleaned up in the event of an exception. The release action is called regardless of whether the body action throws or returns.

Signature

export const bracket = <L, A, B>(
  acquire: TaskEither<L, A>,
  use: (a: A) => TaskEither<L, B>,
  release: (a: A, e: Either<L, B>) => TaskEither<L, void>
): TaskEither<L, B> => ...

Added in v1.10.0

fromEither (function)

Signature

export const fromEither = <L, A>(fa: Either<L, A>): TaskEither<L, A> => ...

Added in v1.0.0

fromIO (function)

Signature

export const fromIO = <L, A>(fa: IO<A>): TaskEither<L, A> => ...

Added in v1.5.0

fromIOEither (function)

Signature

export const fromIOEither = <L, A>(fa: IOEither<L, A>): TaskEither<L, A> => ...

Added in v1.6.0

fromLeft (function)

Signature

export const fromLeft = <L, A>(l: L): TaskEither<L, A> => ...

Added in v1.3.0

fromPredicate (function)

Signature

export function fromPredicate<L, A, B extends A>(
  predicate: Refinement<A, B>,
  onFalse: (a: A) => L
): ((a: A) => TaskEither<L, B>)
export function fromPredicate<L, A>(predicate: Predicate<A>, onFalse: (a: A) => L): ((a: A) => TaskEither<L, A>) { ... }

Added in v1.6.0

getApplyMonoid (function)

Signature

export const getApplyMonoid = <L, A>(M: Monoid<A>): Monoid<TaskEither<L, A>> => ...

Added in v1.9.0

getApplySemigroup (function)

Signature

export const getApplySemigroup = <L, A>(S: Semigroup<A>): Semigroup<TaskEither<L, A>> => ...

Added in v1.9.0

getSemigroup (function)

Signature

export const getSemigroup = <L, A>(S: Semigroup<A>): Semigroup<TaskEither<L, A>> => ...

Added in v1.9.0

left (function)

Signature

export const left = <L, A>(fl: Task<L>): TaskEither<L, A> => ...

Added in v1.0.0

right (function)

Signature

export const right = <L, A>(fa: Task<A>): TaskEither<L, A> => ...

Added in v1.0.0

taskify (function)

Convert a node style callback function to one returning a TaskEither

Note. If the function f admits multiple overloadings, taskify will pick last one. If you want a different behaviour, add an explicit type annotation

// readFile admits multiple overloadings

// const readFile: (a: string) => TaskEither<NodeJS.ErrnoException, Buffer>
const readFile = taskify(fs.readFile)

const readFile2: (filename: string, encoding: string) => TaskEither<NodeJS.ErrnoException, Buffer> = taskify(
  fs.readFile
)

Signature

export function taskify<L, R>(f: (cb: (e: L | null | undefined, r?: R) => void) => void): () => TaskEither<L, R>
export function taskify<A, L, R>(
  f: (a: A, cb: (e: L | null | undefined, r?: R) => void) => void
): (a: A) => TaskEither<L, R>
export function taskify<A, B, L, R>(
  f: (a: A, b: B, cb: (e: L | null | undefined, r?: R) => void) => void
): (a: A, b: B) => TaskEither<L, R>
export function taskify<A, B, C, L, R>(
  f: (a: A, b: B, c: C, cb: (e: L | null | undefined, r?: R) => void) => void
): (a: A, b: B, c: C) => TaskEither<L, R>
export function taskify<A, B, C, D, L, R>(
  f: (a: A, b: B, c: C, d: D, cb: (e: L | null | undefined, r?: R) => void) => void
): (a: A, b: B, c: C, d: D) => TaskEither<L, R>
export function taskify<A, B, C, D, E, L, R>(
  f: (a: A, b: B, c: C, d: D, e: E, cb: (e: L | null | undefined, r?: R) => void) => void
): (a: A, b: B, c: C, d: D, e: E) => TaskEither<L, R> { ... }

Example

import { taskify } from 'fp-ts/lib/TaskEither'
import * as fs from 'fs'

// const stat: (a: string | Buffer) => TaskEither<NodeJS.ErrnoException, fs.Stats>
const stat = taskify(fs.stat)
assert.strictEqual(stat.length, 0)

Added in v1.5.0

tryCatch (function)

Transforms a Promise into a TaskEither, catching the possible error.

Signature

export const tryCatch = <L, A>(f: Lazy<Promise<A>>, onrejected: (reason: unknown) => L): TaskEither<L, A> => ...

Example

import { createHash } from 'crypto'
import { TaskEither, tryCatch } from 'fp-ts/lib/TaskEither'
import { createReadStream } from 'fs'
import { left } from 'fp-ts/lib/Either'

const md5 = (path: string): TaskEither<string, string> => {
  const mkHash = (p: string) =>
    new Promise<string>((resolve, reject) => {
      const hash = createHash('md5')
      const rs = createReadStream(p)
      rs.on('error', (error: Error) => reject(error.message))
      rs.on('data', (chunk: string) => hash.update(chunk))
      rs.on('end', () => {
        return resolve(hash.digest('hex'))
      })
    })
  return tryCatch(() => mkHash(path), message => `cannot create md5 hash: ${String(message)}`)
}

md5('foo')
  .run()
  .then(x => {
    assert.deepStrictEqual(x, left(`cannot create md5 hash: ENOENT: no such file or directory, open 'foo'`))
  })

Added in v1.0.0