Skip to content

Implementing Promise.allSettled #1754

Answered by gcanti
THOUSAND-SKY asked this question in Q&A
Discussion options

You must be logged in to vote
import { array, task, taskEither } from 'fp-ts'
import { pipe } from 'fp-ts/function'
;(async () => {
  console.log(
    await pipe(
      [1, 2, 3, 4],
      array.map((x) =>
        x > 2 ? taskEither.left<number, number>(x) : taskEither.right(x)
      ),
      array.sequence(task.ApplicativePar)
    )()
  )
})()

p.s.

Note that map + sequence = traverse

import { array, task, taskEither } from 'fp-ts'
import { pipe } from 'fp-ts/function'
;(async () => {
  console.log(
    await pipe(
      [1, 2, 3, 4],
      array.traverse(task.ApplicativePar)((x) =>
        x > 2 ? taskEither.left<number, number>(x) : taskEither.right(x)
      )
    )()
  )
})()

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@THOUSAND-SKY
Comment options

Answer selected by THOUSAND-SKY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants