Skip to main content

Task

Instances

Combinators

Constructors

Conversions

Do notation

Instance methods

Legacy

Lifting

Mapping

Model

Sequencing

Traversing

Type lambdas

Utilities

Instances

ApplicativePar

Runs computations in parallel.

Signature

export declare const ApplicativePar: Applicative1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

ApplicativeSeq

Runs computations sequentially.

Signature

export declare const ApplicativeSeq: Applicative1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

ApplyPar

Runs computations in parallel.

Signature

export declare const ApplyPar: Apply1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

ApplySeq

Runs computations sequentially.

Signature

export declare const ApplySeq: Apply1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Chain

Signature

export declare const Chain: chainable.Chain1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

ChainRec

ChainRec for Task

Signature

export declare const ChainRec: ChainRec1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2022-present Jacob Alford

FromIO

Signature

export declare const FromIO: FromIO1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

FromTask

Signature

export declare const FromTask: FromTask1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Functor

Signature

export declare const Functor: Functor1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Monad

Signature

export declare const Monad: Monad1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

MonadIO

Signature

export declare const MonadIO: MonadIO1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

MonadTask

Signature

export declare const MonadTask: MonadTask1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Pointed

Signature

export declare const Pointed: Pointed1<URI>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

getRaceMonoid

Monoid returning the first completed task.

Note: uses Promise.race internally.

Signature

export declare function getRaceMonoid<A = never>(): Monoid<Task<A>>

Details

  • Added in 0.1.0

Example

import * as T from '@fp-tx/core/Task'

async function test() {
const S = T.getRaceMonoid<string>()
const fa = T.delay(20)(T.of('a'))
const fb = T.delay(10)(T.of('b'))
assert.deepStrictEqual(await S.concat(fa, fb)(), 'b')
}

test()

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Combinators

tap

Composes computations in sequence, using the return value of one computation to determine the next computation and keeping only the result of the first.

Signature

export declare const tap: {
<A, _>(self: Task<A>, f: (a: A) => Task<_>): Task<A>
<A, _>(f: (a: A) => Task<_>): (self: Task<A>) => Task<A>
}

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

tapIO

Composes computations in sequence, using the return value of one computation to determine the next computation and keeping only the result of the first.

Signature

export declare const tapIO: {
<A, _>(f: (a: A) => IO<_>): (self: Task<A>) => Task<A>
<A, _>(self: Task<A>, f: (a: A) => IO<_>): Task<A>
}

Details

  • Added in 0.1.0

Example

import { pipe } from '@fp-tx/core/function'
import * as T from '@fp-tx/core/Task'
import * as Console from '@fp-tx/core/Console'

// Will produce `Hello, fp-ts` to the stdout
const effect = pipe(
T.of('fp-ts'),
T.tapIO(value => Console.log(`Hello, ${value}`)),
)

async function test() {
assert.deepStrictEqual(await effect(), 'fp-ts')
}

test()

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Constructors

of

Signature

export declare const of: <A>(a: A) => Task<A>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Conversions

fromIO

Signature

export declare const fromIO: <A>(fa: IO<A>) => Task<A>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Do notation

Do

Signature

export declare const Do: Task<{}>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

apS

Signature

export declare const apS: <N extends string, A, B>(
name: Exclude<N, keyof A>,
fb: Task<B>,
) => (fa: Task<A>) => Task<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

bind

Signature

export declare const bind: <N extends string, A, B>(
name: Exclude<N, keyof A>,
f: (a: A) => Task<B>,
) => (ma: Task<A>) => Task<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

bindTo

Signature

export declare const bindTo: <N extends string>(name: N) => <A>(fa: Task<A>) => Task<{ readonly [K in N]: A }>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Instance methods

chainRec

Signature

export declare const chainRec: ChainRec1<URI>['chainRec']

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2022-present Jacob Alford

Legacy

chain

Alias of flatMap.

Signature

export declare const chain: <A, B>(f: (a: A) => Task<B>) => (ma: Task<A>) => Task<B>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

chainFirst

Alias of tap.

Signature

export declare const chainFirst: <A, B>(f: (a: A) => Task<B>) => (first: Task<A>) => Task<A>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

chainFirstIOK

Alias of tapIO.

Signature

export declare const chainFirstIOK: <A, B>(f: (a: A) => IO<B>) => (first: Task<A>) => Task<A>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

chainIOK

Alias of flatMapIO.

Signature

export declare const chainIOK: <A, B>(f: (a: A) => IO<B>) => (first: Task<A>) => Task<B>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Lifting

fromIOK

Signature

export declare const fromIOK: <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => IO<B>) => (...a: A) => Task<B>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Mapping

as

Maps the value to the specified constant value.

Signature

export declare const as: {
<A>(a: A): <_>(self: Task<_>) => Task<A>
<_, A>(self: Task<_>, a: A): Task<A>
}

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

asUnit

Maps the value to the void constant value.

Signature

export declare const asUnit: <_>(self: Task<_>) => Task<void>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

flap

Signature

export declare const flap: <A>(a: A) => <B>(fab: Task<(a: A) => B>) => Task<B>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

map

map can be used to turn functions (a: A) => B into functions (fa: F<A>) => F<B> whose argument and return types use the type constructor F to represent some computational context.

Signature

export declare const map: <A, B>(f: (a: A) => B) => (fa: Task<A>) => Task<B>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Model

Task

Signature

export interface Task<A> {
(): Promise<A>
}

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Sequencing

flatMap

Signature

export declare const flatMap: {
<A, B>(f: (a: A) => Task<B>): (ma: Task<A>) => Task<B>
<A, B>(ma: Task<A>, f: (a: A) => Task<B>): Task<B>
}

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

flatMapIO

Signature

export declare const flatMapIO: {
<A, B>(f: (a: A) => IO<B>): (self: Task<A>) => Task<B>
<A, B>(self: Task<A>, f: (a: A) => IO<B>): Task<B>
}

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

flatten

Signature

export declare const flatten: <A>(mma: Task<Task<A>>) => Task<A>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Traversing

sequenceArray

Equivalent to ReadonlyArray#sequence(Applicative).

Signature

export declare const sequenceArray: <A>(arr: ReadonlyArray<Task<A>>) => Task<ReadonlyArray<A>>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

sequenceSeqArray

Equivalent to ReadonlyArray#sequence(ApplicativeSeq).

Signature

export declare const sequenceSeqArray: <A>(arr: ReadonlyArray<Task<A>>) => Task<ReadonlyArray<A>>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseArray

Equivalent to ReadonlyArray#traverse(Applicative).

Signature

export declare const traverseArray: <A, B>(f: (a: A) => Task<B>) => (as: readonly A[]) => Task<readonly B[]>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseArrayWithIndex

Equivalent to ReadonlyArray#traverseWithIndex(Applicative).

Signature

export declare const traverseArrayWithIndex: <A, B>(
f: (index: number, a: A) => Task<B>,
) => (as: ReadonlyArray<A>) => Task<ReadonlyArray<B>>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseReadonlyArrayWithIndex

Equivalent to ReadonlyArray#traverseWithIndex(ApplicativePar).

Signature

export declare const traverseReadonlyArrayWithIndex: <A, B>(
f: (index: number, a: A) => Task<B>,
) => (as: readonly A[]) => Task<readonly B[]>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseReadonlyArrayWithIndexSeq

Equivalent to ReadonlyArray#traverseWithIndex(ApplicativeSeq).

Signature

export declare const traverseReadonlyArrayWithIndexSeq: <A, B>(
f: (index: number, a: A) => Task<B>,
) => (as: readonly A[]) => Task<readonly B[]>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseReadonlyNonEmptyArrayWithIndex

Equivalent to ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar).

Signature

export declare const traverseReadonlyNonEmptyArrayWithIndex: <A, B>(
f: (index: number, a: A) => Task<B>,
) => (as: ReadonlyNonEmptyArray<A>) => Task<ReadonlyNonEmptyArray<B>>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseReadonlyNonEmptyArrayWithIndexSeq

Equivalent to ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq).

Signature

export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: <A, B>(
f: (index: number, a: A) => Task<B>,
) => (as: ReadonlyNonEmptyArray<A>) => Task<ReadonlyNonEmptyArray<B>>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseSeqArray

Equivalent to ReadonlyArray#traverse(ApplicativeSeq).

Signature

export declare const traverseSeqArray: <A, B>(f: (a: A) => Task<B>) => (as: readonly A[]) => Task<readonly B[]>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

traverseSeqArrayWithIndex

Equivalent to ReadonlyArray#traverseWithIndex(ApplicativeSeq).

Signature

export declare const traverseSeqArrayWithIndex: <A, B>(
f: (index: number, a: A) => Task<B>,
) => (as: ReadonlyArray<A>) => Task<ReadonlyArray<B>>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Type lambdas

URI

Signature

export type URI = typeof URI

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

URI

Signature

export declare const URI = 'Task'

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

Utilities

ApT

Signature

export declare const ApT: Task<readonly []>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

ap

Signature

export declare const ap: <A>(fa: Task<A>) => <B>(fab: Task<(a: A) => B>) => Task<B>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

apFirst

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

Signature

export declare const apFirst: <B>(second: Task<B>) => <A>(first: Task<A>) => Task<A>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

apSecond

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

Signature

export declare const apSecond: <B>(second: Task<B>) => <A>(first: Task<A>) => Task<B>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

delay

Creates a task that will complete after a time delay

Signature

export declare function delay(millis: number): <A>(ma: Task<A>) => Task<A>

Details

  • Added in 0.1.0

Example

import { sequenceT } from '@fp-tx/core/Apply'
import * as T from '@fp-tx/core/Task'
import { takeRight } from '@fp-tx/core/Array'

async function test() {
const log: Array<string> = []
const append = (message: string): T.Task<void> =>
T.fromIO(() => {
log.push(message)
})
const fa = append('a')
const fb = T.delay(20)(append('b'))
const fc = T.delay(10)(append('c'))
const fd = append('d')
await sequenceT(T.ApplyPar)(fa, fb, fc, fd)()
assert.deepStrictEqual(takeRight(2)(log), ['c', 'b'])
}

test()

License

  • MIT – Copyright (c) 2017-present Giulio Canti

do

Signature

export declare const do_: <MA extends TaskIterable<any>, A>(
yieldFunction: (unwrap: <A>(ma: Task<A>) => TaskIterable<A>) => Generator<MA, A>,
) => Task<A>

fromTask

Signature

export declare const fromTask: <A>(fa: Task<A>) => Task<A>

Details

  • Added in 0.1.0
  • Deprecated

License

  • MIT – Copyright (c) 2017-present Giulio Canti

getMonoid

Use getApplicativeMonoid instead.

Lift a monoid into 'Task', the inner values are concatenated using the provided Monoid.

Signature

export declare const getMonoid: <A>(M: Monoid<A>) => Monoid<Task<A>>

Details

  • Added in 0.1.0
  • Deprecated

License

  • MIT – Copyright (c) 2017-present Giulio Canti

getSemigroup

Use getApplySemigroup instead.

Signature

export declare const getSemigroup: <A>(S: Semigroup<A>) => Semigroup<Task<A>>

Details

  • Added in 0.1.0
  • Deprecated

License

  • MIT – Copyright (c) 2017-present Giulio Canti

let

Signature

export declare const let_: <N extends string, A, B>(
name: Exclude<N, keyof A>,
f: (a: A) => B,
) => (fa: Task<A>) => Task<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>

never

A Task that never completes.

Signature

export declare const never: Task<never>

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

task

This instance is deprecated, use small, specific instances instead. For example if a function needs a Functor instance, pass T.Functor instead of T.task (where T is from import T from 'fp-ts/Task')

Signature

export declare const task: Monad1<URI> & MonadTask1<URI>

Details

  • Added in 0.1.0
  • Deprecated

License

  • MIT – Copyright (c) 2017-present Giulio Canti

taskSeq

This instance is deprecated, use small, specific instances instead. For example if a function needs a Functor instance, pass T.Functor instead of T.taskSeq (where T is from import T from 'fp-ts/Task')

Signature

export declare const taskSeq: Monad1<URI> & MonadTask1<URI>

Details

  • Added in 0.1.0
  • Deprecated

License

  • MIT – Copyright (c) 2017-present Giulio Canti