← Back

Source

taskEither

Monad2<URI> & Bifunctor2<URI> & Alt2<URI> & MonadIO2<URI> & MonadTask2<URI>

Added in v1.0.0 (instance)

taskEitherSeq

typeof taskEither

Added in v1.10.0 (instance)

Like taskEither but ap is sequential

TaskEither

constructor(readonly value: Task<Either<L, A>>) {}

Added in v1.0.0 (data)

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.

alt

(fy: TaskEither<L, A>): TaskEither<L, A>

Added in v1.6.0 (method)

ap

<B>(fab: TaskEither<L, (a: A) => B>): TaskEither<L, B>

Added in v1.0.0 (method)

ap_

<B, C>(this: TaskEither<L, (b: B) => C>, fb: TaskEither<L, B>): TaskEither<L, C>

Added in v1.0.0 (method)

Flipped version of ap

applyFirst

<B>(fb: TaskEither<L, B>): TaskEither<L, A>

Added in v1.6.0 (method)

Combine two effectful actions, keeping only the result of the first

applySecond

<B>(fb: TaskEither<L, B>): TaskEither<L, B>

Added in v1.5.0 (method)

Combine two effectful actions, keeping only the result of the second

attempt

<M = L>(): TaskEither<M, Either<L, A>>

Added in v1.10.0 (method)

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

bimap

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

Added in v1.2.0 (method)

chain

<B>(f: (a: A) => TaskEither<L, B>): TaskEither<L, B>

Added in v1.0.0 (method)

fold

<R>(whenLeft: (l: L) => R, whenRight: (a: A) => R): Task<R>

Added in v1.0.0 (method)

foldTask

<R>(whenLeft: (l: L) => Task<R>, whenRight: (a: A) => Task<R>): Task<R>

Added in v1.10.0 (method)

Similar to fold, but the result is flattened.

foldTaskEither

<M, B>(whenLeft: (l: L) => TaskEither<M, B>, whenRight: (a: A) => TaskEither<M, B>): TaskEither<M, B>

Added in v1.10.0 (method)

Similar to fold, but the result is flattened.

map

<B>(f: (a: A) => B): TaskEither<L, B>

Added in v1.0.0 (method)

mapLeft

<M>(f: (l: L) => M): TaskEither<M, A>

Added in v1.0.0 (method)

orElse

<M>(f: (l: L) => TaskEither<M, A>): TaskEither<M, A>

Added in v1.0.0 (method)

Transforms the failure value of the TaskEither into a new TaskEither

run

(): Promise<Either<L, A>>

Added in v1.0.0 (method)

Runs the inner Task

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 (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.

fromEither

<L, A>(fa: Either<L, A>): TaskEither<L, A>

Added in v1.0.0 (function)

fromIO

<L, A>(fa: IO<A>): TaskEither<L, A>

Added in v1.5.0 (function)

fromIOEither

<L, A>(fa: IOEither<L, A>): TaskEither<L, A>

Added in v1.6.0 (function)

fromLeft

<L, A>(l: L): TaskEither<L, A>

Added in v1.3.0 (function)

fromPredicate

<L, A>(predicate: Predicate<A>, whenFalse: (a: A) => L): ((a: A) => TaskEither<L, A>)

Added in v1.6.0 (function)

getApplyMonoid

<L, A>(M: Monoid<A>): Monoid<TaskEither<L, A>>

Added in v1.9.0 (function)

getApplySemigroup

<L, A>(S: Semigroup<A>): Semigroup<TaskEither<L, A>>

Added in v1.9.0 (function)

getSemigroup

<L, A>(S: Semigroup<A>): Semigroup<TaskEither<L, A>>

Added in v1.9.0 (function)

left

<L, A>(fa: Task<L>): TaskEither<L, A>

Added in v1.0.0 (function)

<L, A>(fa: Task<A>): TaskEither<L, A>

Added in v1.0.0 (function)

taskify

taskify<L, R>(f: Function): () => TaskEither<L, R>

Added in v1.5.0 (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
)

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)

tryCatch

<L, A>(f: Lazy<Promise<A>>, onrejected: (reason: {}) => L): TaskEither<L, A>

Added in v1.0.0 (function)

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

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.deepEqual(x, left(`cannot create md5 hash: ENOENT: no such file or directory, open 'foo'`))
  })