Task overview
interface Task<A> {
(): Promise<A>
}
Task<A>
represents an asynchronous computation that yields a value of type A
and never fails. If you want to represent an asynchronous computation that may fail, please see TaskEither
.
Added in v2.0.0
Table of contents
- combinators
- constructors
- conversions
- do notation
- instances
- legacy
- lifting
- mapping
- model
- sequencing
- traversing
- type lambdas
- utils
- zone of death
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>
}
Added in v2.15.0
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>
}
Example
import { pipe } from 'fp-ts/function'
import * as T from 'fp-ts/Task'
import * as Console from 'fp-ts/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()
Added in v2.16.0
constructors
of
Signature
export declare const of: <A>(a: A) => Task<A>
Added in v2.0.0
conversions
fromIO
Signature
export declare const fromIO: <A>(fa: IO<A>) => Task<A>
Added in v2.0.0
do notation
Do
Signature
export declare const Do: Task<{}>
Added in v2.9.0
apS
Signature
export declare const apS: <N, 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 }>
Added in v2.8.0
bind
Signature
export declare const bind: <N, 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 }>
Added in v2.8.0
bindTo
Signature
export declare const bindTo: <N>(name: N) => <A>(fa: Task<A>) => Task<{ readonly [K in N]: A }>
Added in v2.8.0
let
Signature
export declare const let: <N, 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 }>
Added in v2.13.0
instances
ApplicativePar
Runs computations in parallel.
Signature
export declare const ApplicativePar: Applicative1<'Task'>
Added in v2.7.0
ApplicativeSeq
Runs computations sequentially.
Signature
export declare const ApplicativeSeq: Applicative1<'Task'>
Added in v2.7.0
ApplyPar
Runs computations in parallel.
Signature
export declare const ApplyPar: Apply1<'Task'>
Added in v2.10.0
ApplySeq
Runs computations sequentially.
Signature
export declare const ApplySeq: Apply1<'Task'>
Added in v2.10.0
Chain
Signature
export declare const Chain: chainable.Chain1<'Task'>
Added in v2.10.0
FromIO
Signature
export declare const FromIO: FromIO1<'Task'>
Added in v2.10.0
FromTask
Signature
export declare const FromTask: FromTask1<'Task'>
Added in v2.10.0
Functor
Signature
export declare const Functor: Functor1<'Task'>
Added in v2.7.0
Monad
Signature
export declare const Monad: Monad1<'Task'>
Added in v2.10.0
MonadIO
Signature
export declare const MonadIO: MonadIO1<'Task'>
Added in v2.10.0
MonadTask
Signature
export declare const MonadTask: MonadTask1<'Task'>
Added in v2.10.0
Pointed
Signature
export declare const Pointed: Pointed1<'Task'>
Added in v2.10.0
getRaceMonoid
Monoid returning the first completed task.
Note: uses Promise.race
internally.
Signature
export declare function getRaceMonoid<A = never>(): Monoid<Task<A>>
Example
import * as T from 'fp-ts/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()
Added in v2.0.0
legacy
chain
Alias of flatMap
.
Signature
export declare const chain: <A, B>(f: (a: A) => Task<B>) => (ma: Task<A>) => Task<B>
Added in v2.0.0
chainFirst
Alias of tap
.
Signature
export declare const chainFirst: <A, B>(f: (a: A) => Task<B>) => (first: Task<A>) => Task<A>
Added in v2.0.0
chainFirstIOK
Alias of tapIO
.
Signature
export declare const chainFirstIOK: <A, B>(f: (a: A) => IO<B>) => (first: Task<A>) => Task<A>
Added in v2.10.0
chainIOK
Alias of flatMapIO
.
Signature
export declare const chainIOK: <A, B>(f: (a: A) => IO<B>) => (first: Task<A>) => Task<B>
Added in v2.4.0
lifting
fromIOK
Signature
export declare const fromIOK: <A extends readonly unknown[], B>(f: (...a: A) => IO<B>) => (...a: A) => Task<B>
Added in v2.4.0
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> }
Added in v2.16.0
asUnit
Maps the value to the void constant value.
Signature
export declare const asUnit: <_>(self: Task<_>) => Task<void>
Added in v2.16.0
flap
Signature
export declare const flap: <A>(a: A) => <B>(fab: Task<(a: A) => B>) => Task<B>
Added in v2.10.0
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>
Added in v2.0.0
model
Task (interface)
Signature
export interface Task<A> {
(): Promise<A>
}
Added in v2.0.0
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>
}
Added in v2.14.0
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>
}
Added in v2.16.0
flatten
Signature
export declare const flatten: <A>(mma: Task<Task<A>>) => Task<A>
Added in v2.0.0
traversing
sequenceArray
Equivalent to ReadonlyArray#sequence(Applicative)
.
Signature
export declare const sequenceArray: <A>(arr: readonly Task<A>[]) => Task<readonly A[]>
Added in v2.9.0
sequenceSeqArray
Equivalent to ReadonlyArray#sequence(ApplicativeSeq)
.
Signature
export declare const sequenceSeqArray: <A>(arr: readonly Task<A>[]) => Task<readonly A[]>
Added in v2.9.0
traverseArray
Equivalent to ReadonlyArray#traverse(Applicative)
.
Signature
export declare const traverseArray: <A, B>(f: (a: A) => Task<B>) => (as: readonly A[]) => Task<readonly B[]>
Added in v2.9.0
traverseArrayWithIndex
Equivalent to ReadonlyArray#traverseWithIndex(Applicative)
.
Signature
export declare const traverseArrayWithIndex: <A, B>(
f: (index: number, a: A) => Task<B>
) => (as: readonly A[]) => Task<readonly B[]>
Added in v2.9.0
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[]>
Added in v2.11.0
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[]>
Added in v2.11.0
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>>
Added in v2.11.0
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>>
Added in v2.11.0
traverseSeqArray
Equivalent to ReadonlyArray#traverse(ApplicativeSeq)
.
Signature
export declare const traverseSeqArray: <A, B>(f: (a: A) => Task<B>) => (as: readonly A[]) => Task<readonly B[]>
Added in v2.9.0
traverseSeqArrayWithIndex
Equivalent to ReadonlyArray#traverseWithIndex(ApplicativeSeq)
.
Signature
export declare const traverseSeqArrayWithIndex: <A, B>(
f: (index: number, a: A) => Task<B>
) => (as: readonly A[]) => Task<readonly B[]>
Added in v2.9.0
type lambdas
URI
Signature
export declare const URI: 'Task'
Added in v2.0.0
URI (type alias)
Signature
export type URI = typeof URI
Added in v2.0.0
utils
ApT
Signature
export declare const ApT: Task<readonly []>
Added in v2.11.0
ap
Signature
export declare const ap: <A>(fa: Task<A>) => <B>(fab: Task<(a: A) => B>) => Task<B>
Added in v2.0.0
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>
Added in v2.0.0
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>
Added in v2.0.0
delay
Creates a task that will complete after a time delay
Signature
export declare function delay(millis: number): <A>(ma: Task<A>) => Task<A>
Example
import { sequenceT } from 'fp-ts/Apply'
import * as T from 'fp-ts/Task'
import { takeRight } from 'fp-ts/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()
Added in v2.0.0
never
A Task
that never completes.
Signature
export declare const never: Task<never>
Added in v2.0.0
zone of death
fromTask
Signature
export declare const fromTask: <A>(fa: Task<A>) => Task<A>
Added in v2.7.0
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>>
Added in v2.0.0
getSemigroup
Use getApplySemigroup
instead.
Signature
export declare const getSemigroup: <A>(S: Semigroup<A>) => Semigroup<Task<A>>
Added in v2.0.0
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<'Task'> & MonadTask1<'Task'>
Added in v2.0.0
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<'Task'> & MonadTask1<'Task'>
Added in v2.0.0