523 lines
13 KiB
JavaScript
523 lines
13 KiB
JavaScript
/**
|
|
* ```ts
|
|
* 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`.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
import { getApplicativeMonoid } from './Applicative';
|
|
import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_, getApplySemigroup as getApplySemigroup_ } from './Apply';
|
|
import { bind as bind_, chainFirst as chainFirst_ } from './Chain';
|
|
import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, fromIOK as fromIOK_ } from './FromIO';
|
|
import { identity, pipe } from './function';
|
|
import { let as let__, bindTo as bindTo_, flap as flap_ } from './Functor';
|
|
import * as _ from './internal';
|
|
// -------------------------------------------------------------------------------------
|
|
// conversions
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* @category conversions
|
|
* @since 2.0.0
|
|
*/
|
|
export var fromIO = function (ma) { return function () { return Promise.resolve().then(ma); }; };
|
|
// -------------------------------------------------------------------------------------
|
|
// combinators
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Creates a task that will complete after a time delay
|
|
*
|
|
* @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()
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
export function delay(millis) {
|
|
return function (ma) { return function () {
|
|
return new Promise(function (resolve) {
|
|
setTimeout(function () {
|
|
Promise.resolve().then(ma).then(resolve);
|
|
}, millis);
|
|
});
|
|
}; };
|
|
}
|
|
var _map = function (fa, f) { return pipe(fa, map(f)); };
|
|
var _apPar = function (fab, fa) { return pipe(fab, ap(fa)); };
|
|
var _apSeq = function (fab, fa) {
|
|
return pipe(fab, chain(function (f) { return pipe(fa, map(f)); }));
|
|
};
|
|
var _chain = function (ma, f) { return pipe(ma, chain(f)); };
|
|
/**
|
|
* `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.
|
|
*
|
|
* @category mapping
|
|
* @since 2.0.0
|
|
*/
|
|
export var map = function (f) { return function (fa) { return function () {
|
|
return Promise.resolve().then(fa).then(f);
|
|
}; }; };
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
export var ap = function (fa) { return function (fab) { return function () {
|
|
return Promise.all([Promise.resolve().then(fab), Promise.resolve().then(fa)]).then(function (_a) {
|
|
var f = _a[0], a = _a[1];
|
|
return f(a);
|
|
});
|
|
}; }; };
|
|
/**
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
export var of = function (a) { return function () { return Promise.resolve(a); }; };
|
|
/**
|
|
* Composes computations in sequence, using the return value of one computation to determine the next computation.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
export var chain = function (f) { return function (ma) { return function () {
|
|
return Promise.resolve()
|
|
.then(ma)
|
|
.then(function (a) { return f(a)(); });
|
|
}; }; };
|
|
/**
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
export var flatten = /*#__PURE__*/ chain(identity);
|
|
/**
|
|
* @category type lambdas
|
|
* @since 2.0.0
|
|
*/
|
|
export var URI = 'Task';
|
|
/**
|
|
* Monoid returning the first completed task.
|
|
*
|
|
* Note: uses `Promise.race` internally.
|
|
*
|
|
* @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()
|
|
*
|
|
* @category instances
|
|
* @since 2.0.0
|
|
*/
|
|
export function getRaceMonoid() {
|
|
return {
|
|
concat: function (x, y) { return function () { return Promise.race([Promise.resolve().then(x), Promise.resolve().then(y)]); }; },
|
|
empty: never
|
|
};
|
|
}
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Functor = {
|
|
URI: URI,
|
|
map: _map
|
|
};
|
|
/**
|
|
* @category mapping
|
|
* @since 2.10.0
|
|
*/
|
|
export var flap = /*#__PURE__*/ flap_(Functor);
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Pointed = {
|
|
URI: URI,
|
|
of: of
|
|
};
|
|
/**
|
|
* Runs computations in parallel.
|
|
*
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var ApplyPar = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _apPar
|
|
};
|
|
/**
|
|
* Combine two effectful actions, keeping only the result of the first.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
export var apFirst = /*#__PURE__*/ apFirst_(ApplyPar);
|
|
/**
|
|
* Combine two effectful actions, keeping only the result of the second.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
export var apSecond = /*#__PURE__*/ apSecond_(ApplyPar);
|
|
/**
|
|
* Runs computations in parallel.
|
|
*
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var ApplicativePar = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _apPar,
|
|
of: of
|
|
};
|
|
/**
|
|
* Runs computations sequentially.
|
|
*
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var ApplySeq = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _apSeq
|
|
};
|
|
/**
|
|
* Runs computations sequentially.
|
|
*
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var ApplicativeSeq = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _apSeq,
|
|
of: of
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Chain = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _apPar,
|
|
chain: _chain
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Monad = {
|
|
URI: URI,
|
|
map: _map,
|
|
of: of,
|
|
ap: _apPar,
|
|
chain: _chain
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var MonadIO = {
|
|
URI: URI,
|
|
map: _map,
|
|
of: of,
|
|
ap: _apPar,
|
|
chain: _chain,
|
|
fromIO: fromIO
|
|
};
|
|
/**
|
|
* @category zone of death
|
|
* @since 2.7.0
|
|
* @deprecated
|
|
*/
|
|
export var fromTask = identity;
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var MonadTask = {
|
|
URI: URI,
|
|
map: _map,
|
|
of: of,
|
|
ap: _apPar,
|
|
chain: _chain,
|
|
fromIO: fromIO,
|
|
fromTask: fromTask
|
|
};
|
|
/**
|
|
* Composes computations in sequence, using the return value of one computation to determine the next computation and
|
|
* keeping only the result of the first.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
export var chainFirst = /*#__PURE__*/ chainFirst_(Chain);
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var FromIO = {
|
|
URI: URI,
|
|
fromIO: fromIO
|
|
};
|
|
/**
|
|
* @category lifting
|
|
* @since 2.4.0
|
|
*/
|
|
export var fromIOK =
|
|
/*#__PURE__*/ fromIOK_(FromIO);
|
|
/**
|
|
* @category sequencing
|
|
* @since 2.4.0
|
|
*/
|
|
export var chainIOK = /*#__PURE__*/ chainIOK_(FromIO, Chain);
|
|
/**
|
|
* @category sequencing
|
|
* @since 2.10.0
|
|
*/
|
|
export var chainFirstIOK = /*#__PURE__*/ chainFirstIOK_(FromIO, Chain);
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var FromTask = {
|
|
URI: URI,
|
|
fromIO: fromIO,
|
|
fromTask: fromTask
|
|
};
|
|
// -------------------------------------------------------------------------------------
|
|
// utils
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* A `Task` that never completes.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
export var never = function () { return new Promise(function (_) { return undefined; }); };
|
|
// -------------------------------------------------------------------------------------
|
|
// do notation
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* @category do notation
|
|
* @since 2.9.0
|
|
*/
|
|
export var Do = /*#__PURE__*/ of(_.emptyRecord);
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
export var bindTo = /*#__PURE__*/ bindTo_(Functor);
|
|
var let_ = /*#__PURE__*/ let__(Functor);
|
|
export {
|
|
/**
|
|
* @category do notation
|
|
* @since 2.13.0
|
|
*/
|
|
let_ as let };
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
export var bind = /*#__PURE__*/ bind_(Chain);
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
export var apS = /*#__PURE__*/ apS_(ApplyPar);
|
|
/**
|
|
* @since 2.11.0
|
|
*/
|
|
export var ApT = /*#__PURE__*/ of(_.emptyReadonlyArray);
|
|
// -------------------------------------------------------------------------------------
|
|
// array utils
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
export var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
|
|
return function (as) {
|
|
return function () {
|
|
return Promise.all(as.map(function (a, i) { return Promise.resolve().then(function () { return f(i, a)(); }); }));
|
|
};
|
|
};
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
export var traverseReadonlyArrayWithIndex = function (f) {
|
|
var g = traverseReadonlyNonEmptyArrayWithIndex(f);
|
|
return function (as) { return (_.isNonEmpty(as) ? g(as) : ApT); };
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
export var traverseReadonlyNonEmptyArrayWithIndexSeq = function (f) {
|
|
return function (as) {
|
|
return function () {
|
|
return _.tail(as).reduce(function (acc, a, i) {
|
|
return acc.then(function (bs) {
|
|
return Promise.resolve()
|
|
.then(f(i + 1, a))
|
|
.then(function (b) {
|
|
bs.push(b);
|
|
return bs;
|
|
});
|
|
});
|
|
}, Promise.resolve()
|
|
.then(f(0, _.head(as)))
|
|
.then(_.singleton));
|
|
};
|
|
};
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
export var traverseReadonlyArrayWithIndexSeq = function (f) {
|
|
var g = traverseReadonlyNonEmptyArrayWithIndexSeq(f);
|
|
return function (as) { return (_.isNonEmpty(as) ? g(as) : ApT); };
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverse(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var traverseArray = function (f) {
|
|
return traverseReadonlyArrayWithIndex(function (_, a) { return f(a); });
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#sequence(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var sequenceArray =
|
|
/*#__PURE__*/ traverseArray(identity);
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var traverseSeqArrayWithIndex = traverseReadonlyArrayWithIndexSeq;
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var traverseSeqArray = function (f) {
|
|
return traverseReadonlyArrayWithIndexSeq(function (_, a) { return f(a); });
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var sequenceSeqArray =
|
|
/*#__PURE__*/ traverseSeqArray(identity);
|
|
// -------------------------------------------------------------------------------------
|
|
// deprecated
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* 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'`)
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var task = {
|
|
URI: URI,
|
|
map: _map,
|
|
of: of,
|
|
ap: _apPar,
|
|
chain: _chain,
|
|
fromIO: fromIO,
|
|
fromTask: fromTask
|
|
};
|
|
/**
|
|
* 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'`)
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var taskSeq = {
|
|
URI: URI,
|
|
map: _map,
|
|
of: of,
|
|
ap: _apSeq,
|
|
chain: _chain,
|
|
fromIO: fromIO,
|
|
fromTask: fromTask
|
|
};
|
|
/**
|
|
* Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getSemigroup = /*#__PURE__*/ getApplySemigroup_(ApplySeq);
|
|
/**
|
|
* Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
|
|
*
|
|
* Lift a monoid into 'Task', the inner values are concatenated using the provided `Monoid`.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getMonoid = /*#__PURE__*/ getApplicativeMonoid(ApplicativeSeq);
|