seasonedcc / composable-functions

Types and functions to make composition easy and safe
MIT License
666 stars 14 forks source link

First combinator #43

Closed gustavoguichard closed 2 years ago

gustavoguichard commented 2 years ago

The first combinator will return the result of the first successful domain function. It handles inputs and environments like the all function. It is important to notice that all domain functions will be executed in parallel so keep an eye on the side effects.

const a = makeDomainFunction(
  z.object({ n: z.number(), operation: z.literal('increment') }),
)(async ({ n }) => n + 1)
const b = makeDomainFunction(
  z.object({ n: z.number(), operation: z.literal('decrement') }),
)(async ({ n }) => n - 1)

const result = await first(a, b)({ n: 1, operation: 'increment' })