版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * ```ts
  3. * interface Task<A> {
  4. * (): Promise<A>
  5. * }
  6. * ```
  7. *
  8. * `Task<A>` represents an asynchronous computation that yields a value of type `A` and **never fails**.
  9. * If you want to represent an asynchronous computation that may fail, please see `TaskEither`.
  10. *
  11. * @since 2.0.0
  12. */
  13. import { Applicative1 } from './Applicative'
  14. import { Apply1 } from './Apply'
  15. import { Chain1 } from './Chain'
  16. import { FromIO1 } from './FromIO'
  17. import { FromTask1 } from './FromTask'
  18. import { Functor1 } from './Functor'
  19. import { Monad1 } from './Monad'
  20. import { MonadIO1 } from './MonadIO'
  21. import { MonadTask1 } from './MonadTask'
  22. import { Monoid } from './Monoid'
  23. import { Pointed1 } from './Pointed'
  24. import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray'
  25. import { Semigroup } from './Semigroup'
  26. import { IO } from './IO'
  27. /**
  28. * @category model
  29. * @since 2.0.0
  30. */
  31. export interface Task<A> {
  32. (): Promise<A>
  33. }
  34. /**
  35. * @category conversions
  36. * @since 2.0.0
  37. */
  38. export declare const fromIO: <A>(fa: IO<A>) => Task<A>
  39. /**
  40. * Creates a task that will complete after a time delay
  41. *
  42. * @example
  43. * import { sequenceT } from 'fp-ts/Apply'
  44. * import * as T from 'fp-ts/Task'
  45. * import { takeRight } from 'fp-ts/Array'
  46. *
  47. * async function test() {
  48. * const log: Array<string> = []
  49. * const append = (message: string): T.Task<void> =>
  50. * T.fromIO(() => {
  51. * log.push(message)
  52. * })
  53. * const fa = append('a')
  54. * const fb = T.delay(20)(append('b'))
  55. * const fc = T.delay(10)(append('c'))
  56. * const fd = append('d')
  57. * await sequenceT(T.ApplyPar)(fa, fb, fc, fd)()
  58. * assert.deepStrictEqual(takeRight(2)(log), ['c', 'b'])
  59. * }
  60. *
  61. * test()
  62. *
  63. * @since 2.0.0
  64. */
  65. export declare function delay(millis: number): <A>(ma: Task<A>) => Task<A>
  66. /**
  67. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  68. * use the type constructor `F` to represent some computational context.
  69. *
  70. * @category mapping
  71. * @since 2.0.0
  72. */
  73. export declare const map: <A, B>(f: (a: A) => B) => (fa: Task<A>) => Task<B>
  74. /**
  75. * @since 2.0.0
  76. */
  77. export declare const ap: <A>(fa: Task<A>) => <B>(fab: Task<(a: A) => B>) => Task<B>
  78. /**
  79. * @category constructors
  80. * @since 2.0.0
  81. */
  82. export declare const of: <A>(a: A) => Task<A>
  83. /**
  84. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  85. *
  86. * @category sequencing
  87. * @since 2.0.0
  88. */
  89. export declare const chain: <A, B>(f: (a: A) => Task<B>) => (ma: Task<A>) => Task<B>
  90. /**
  91. * @category sequencing
  92. * @since 2.0.0
  93. */
  94. export declare const flatten: <A>(mma: Task<Task<A>>) => Task<A>
  95. /**
  96. * @category type lambdas
  97. * @since 2.0.0
  98. */
  99. export declare const URI = 'Task'
  100. /**
  101. * @category type lambdas
  102. * @since 2.0.0
  103. */
  104. export declare type URI = typeof URI
  105. declare module './HKT' {
  106. interface URItoKind<A> {
  107. readonly [URI]: Task<A>
  108. }
  109. }
  110. /**
  111. * Monoid returning the first completed task.
  112. *
  113. * Note: uses `Promise.race` internally.
  114. *
  115. * @example
  116. * import * as T from 'fp-ts/Task'
  117. *
  118. * async function test() {
  119. * const S = T.getRaceMonoid<string>()
  120. * const fa = T.delay(20)(T.of('a'))
  121. * const fb = T.delay(10)(T.of('b'))
  122. * assert.deepStrictEqual(await S.concat(fa, fb)(), 'b')
  123. * }
  124. *
  125. * test()
  126. *
  127. * @category instances
  128. * @since 2.0.0
  129. */
  130. export declare function getRaceMonoid<A = never>(): Monoid<Task<A>>
  131. /**
  132. * @category instances
  133. * @since 2.7.0
  134. */
  135. export declare const Functor: Functor1<URI>
  136. /**
  137. * @category mapping
  138. * @since 2.10.0
  139. */
  140. export declare const flap: <A>(a: A) => <B>(fab: Task<(a: A) => B>) => Task<B>
  141. /**
  142. * @category instances
  143. * @since 2.10.0
  144. */
  145. export declare const Pointed: Pointed1<URI>
  146. /**
  147. * Runs computations in parallel.
  148. *
  149. * @category instances
  150. * @since 2.10.0
  151. */
  152. export declare const ApplyPar: Apply1<URI>
  153. /**
  154. * Combine two effectful actions, keeping only the result of the first.
  155. *
  156. * @since 2.0.0
  157. */
  158. export declare const apFirst: <B>(second: Task<B>) => <A>(first: Task<A>) => Task<A>
  159. /**
  160. * Combine two effectful actions, keeping only the result of the second.
  161. *
  162. * @since 2.0.0
  163. */
  164. export declare const apSecond: <B>(second: Task<B>) => <A>(first: Task<A>) => Task<B>
  165. /**
  166. * Runs computations in parallel.
  167. *
  168. * @category instances
  169. * @since 2.7.0
  170. */
  171. export declare const ApplicativePar: Applicative1<URI>
  172. /**
  173. * Runs computations sequentially.
  174. *
  175. * @category instances
  176. * @since 2.10.0
  177. */
  178. export declare const ApplySeq: Apply1<URI>
  179. /**
  180. * Runs computations sequentially.
  181. *
  182. * @category instances
  183. * @since 2.7.0
  184. */
  185. export declare const ApplicativeSeq: Applicative1<URI>
  186. /**
  187. * @category instances
  188. * @since 2.10.0
  189. */
  190. export declare const Chain: Chain1<URI>
  191. /**
  192. * @category instances
  193. * @since 2.10.0
  194. */
  195. export declare const Monad: Monad1<URI>
  196. /**
  197. * @category instances
  198. * @since 2.10.0
  199. */
  200. export declare const MonadIO: MonadIO1<URI>
  201. /**
  202. * @category zone of death
  203. * @since 2.7.0
  204. * @deprecated
  205. */
  206. export declare const fromTask: <A>(fa: Task<A>) => Task<A>
  207. /**
  208. * @category instances
  209. * @since 2.10.0
  210. */
  211. export declare const MonadTask: MonadTask1<URI>
  212. /**
  213. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  214. * keeping only the result of the first.
  215. *
  216. * @category sequencing
  217. * @since 2.0.0
  218. */
  219. export declare const chainFirst: <A, B>(f: (a: A) => Task<B>) => (first: Task<A>) => Task<A>
  220. /**
  221. * @category instances
  222. * @since 2.10.0
  223. */
  224. export declare const FromIO: FromIO1<URI>
  225. /**
  226. * @category lifting
  227. * @since 2.4.0
  228. */
  229. export declare const fromIOK: <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => IO<B>) => (...a: A) => Task<B>
  230. /**
  231. * @category sequencing
  232. * @since 2.4.0
  233. */
  234. export declare const chainIOK: <A, B>(f: (a: A) => IO<B>) => (first: Task<A>) => Task<B>
  235. /**
  236. * @category sequencing
  237. * @since 2.10.0
  238. */
  239. export declare const chainFirstIOK: <A, B>(f: (a: A) => IO<B>) => (first: Task<A>) => Task<A>
  240. /**
  241. * @category instances
  242. * @since 2.10.0
  243. */
  244. export declare const FromTask: FromTask1<URI>
  245. /**
  246. * A `Task` that never completes.
  247. *
  248. * @since 2.0.0
  249. */
  250. export declare const never: Task<never>
  251. /**
  252. * @category do notation
  253. * @since 2.9.0
  254. */
  255. export declare const Do: Task<{}>
  256. /**
  257. * @category do notation
  258. * @since 2.8.0
  259. */
  260. export declare const bindTo: <N extends string>(name: N) => <A>(fa: Task<A>) => Task<{ readonly [K in N]: A }>
  261. declare const let_: <N extends string, A, B>(
  262. name: Exclude<N, keyof A>,
  263. f: (a: A) => B
  264. ) => (fa: Task<A>) => Task<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  265. export {
  266. /**
  267. * @category do notation
  268. * @since 2.13.0
  269. */
  270. let_ as let
  271. }
  272. /**
  273. * @category do notation
  274. * @since 2.8.0
  275. */
  276. export declare const bind: <N extends string, A, B>(
  277. name: Exclude<N, keyof A>,
  278. f: (a: A) => Task<B>
  279. ) => (ma: Task<A>) => Task<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  280. /**
  281. * @category do notation
  282. * @since 2.8.0
  283. */
  284. export declare const apS: <N extends string, A, B>(
  285. name: Exclude<N, keyof A>,
  286. fb: Task<B>
  287. ) => (fa: Task<A>) => Task<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  288. /**
  289. * @since 2.11.0
  290. */
  291. export declare const ApT: Task<readonly []>
  292. /**
  293. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`.
  294. *
  295. * @category traversing
  296. * @since 2.11.0
  297. */
  298. export declare const traverseReadonlyNonEmptyArrayWithIndex: <A, B>(
  299. f: (index: number, a: A) => Task<B>
  300. ) => (as: ReadonlyNonEmptyArray<A>) => Task<ReadonlyNonEmptyArray<B>>
  301. /**
  302. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`.
  303. *
  304. * @category traversing
  305. * @since 2.11.0
  306. */
  307. export declare const traverseReadonlyArrayWithIndex: <A, B>(
  308. f: (index: number, a: A) => Task<B>
  309. ) => (as: readonly A[]) => Task<readonly B[]>
  310. /**
  311. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`.
  312. *
  313. * @category traversing
  314. * @since 2.11.0
  315. */
  316. export declare const traverseReadonlyNonEmptyArrayWithIndexSeq: <A, B>(
  317. f: (index: number, a: A) => Task<B>
  318. ) => (as: ReadonlyNonEmptyArray<A>) => Task<ReadonlyNonEmptyArray<B>>
  319. /**
  320. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
  321. *
  322. * @category traversing
  323. * @since 2.11.0
  324. */
  325. export declare const traverseReadonlyArrayWithIndexSeq: <A, B>(
  326. f: (index: number, a: A) => Task<B>
  327. ) => (as: readonly A[]) => Task<readonly B[]>
  328. /**
  329. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  330. *
  331. * @category traversing
  332. * @since 2.9.0
  333. */
  334. export declare const traverseArrayWithIndex: <A, B>(
  335. f: (index: number, a: A) => Task<B>
  336. ) => (as: ReadonlyArray<A>) => Task<ReadonlyArray<B>>
  337. /**
  338. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  339. *
  340. * @category traversing
  341. * @since 2.9.0
  342. */
  343. export declare const traverseArray: <A, B>(f: (a: A) => Task<B>) => (as: readonly A[]) => Task<readonly B[]>
  344. /**
  345. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  346. *
  347. * @category traversing
  348. * @since 2.9.0
  349. */
  350. export declare const sequenceArray: <A>(arr: ReadonlyArray<Task<A>>) => Task<ReadonlyArray<A>>
  351. /**
  352. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
  353. *
  354. * @category traversing
  355. * @since 2.9.0
  356. */
  357. export declare const traverseSeqArrayWithIndex: <A, B>(
  358. f: (index: number, a: A) => Task<B>
  359. ) => (as: ReadonlyArray<A>) => Task<ReadonlyArray<B>>
  360. /**
  361. * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`.
  362. *
  363. * @category traversing
  364. * @since 2.9.0
  365. */
  366. export declare const traverseSeqArray: <A, B>(f: (a: A) => Task<B>) => (as: readonly A[]) => Task<readonly B[]>
  367. /**
  368. * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`.
  369. *
  370. * @category traversing
  371. * @since 2.9.0
  372. */
  373. export declare const sequenceSeqArray: <A>(arr: ReadonlyArray<Task<A>>) => Task<ReadonlyArray<A>>
  374. /**
  375. * This instance is deprecated, use small, specific instances instead.
  376. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.task`
  377. * (where `T` is from `import T from 'fp-ts/Task'`)
  378. *
  379. * @category zone of death
  380. * @since 2.0.0
  381. * @deprecated
  382. */
  383. export declare const task: Monad1<URI> & MonadTask1<URI>
  384. /**
  385. * This instance is deprecated, use small, specific instances instead.
  386. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.taskSeq`
  387. * (where `T` is from `import T from 'fp-ts/Task'`)
  388. *
  389. * @category zone of death
  390. * @since 2.0.0
  391. * @deprecated
  392. */
  393. export declare const taskSeq: Monad1<URI> & MonadTask1<URI>
  394. /**
  395. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  396. *
  397. * @category zone of death
  398. * @since 2.0.0
  399. * @deprecated
  400. */
  401. export declare const getSemigroup: <A>(S: Semigroup<A>) => Semigroup<Task<A>>
  402. /**
  403. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  404. *
  405. * Lift a monoid into 'Task', the inner values are concatenated using the provided `Monoid`.
  406. *
  407. * @category zone of death
  408. * @since 2.0.0
  409. * @deprecated
  410. */
  411. export declare const getMonoid: <A>(M: Monoid<A>) => Monoid<Task<A>>