Skip to main content

Function: flatMap()

Call Signature

flatMap<T, U, E, F>(result, fn): Result<U, E | F>

Defined in: result/flatMap/index.ts:40

Chains Result-returning operations (monadic bind).

If the Result is Ok, applies the function to its value and returns the resulting Result. If the Result is Err, returns the Err unchanged.

Use this when you have a sequence of operations that each return a Result. This prevents nesting Results (Result<Result<T, E>, E>).

Type Parameters

T

T

U

U

E

E

F

F

Parameters

result

Result<T, E>

The Result to flatMap over

fn

(value) => Result<U, F>

Function that returns a Result

Returns

Result<U, E | F>

The Result from the function, or the original Err

Example

// Data-first
const parseNumber = (str: string): Result<number, string> =>
str === '' ? err('Empty string') : ok(Number(str))

flatMap(ok('42'), parseNumber) // => Ok(42)
flatMap(ok(''), parseNumber) // => Err('Empty string')
flatMap(err('fail'), parseNumber) // => Err('fail')

// Data-last (in pipe) - chaining operations
pipe(
tryCatch(() => readFile('config.json')),
flatMap(str => tryCatch(() => JSON.parse(str))),
flatMap(config => validateConfig(config)),
map(config => config.port)
)

See

  • map - for transforming values without nesting
  • flatten - for flattening nested Results

Call Signature

flatMap<T, U, F>(fn): <E>(result) => Result<U, F | E>

Defined in: result/flatMap/index.ts:44

Chains Result-returning operations (monadic bind).

If the Result is Ok, applies the function to its value and returns the resulting Result. If the Result is Err, returns the Err unchanged.

Use this when you have a sequence of operations that each return a Result. This prevents nesting Results (Result<Result<T, E>, E>).

Type Parameters

T

T

U

U

F

F

Parameters

fn

(value) => Result<U, F>

Function that returns a Result

Returns

The Result from the function, or the original Err

<E>(result): Result<U, F | E>

Type Parameters

E

E

Parameters

result

Result<T, E>

Returns

Result<U, F | E>

Example

// Data-first
const parseNumber = (str: string): Result<number, string> =>
str === '' ? err('Empty string') : ok(Number(str))

flatMap(ok('42'), parseNumber) // => Ok(42)
flatMap(ok(''), parseNumber) // => Err('Empty string')
flatMap(err('fail'), parseNumber) // => Err('fail')

// Data-last (in pipe) - chaining operations
pipe(
tryCatch(() => readFile('config.json')),
flatMap(str => tryCatch(() => JSON.parse(str))),
flatMap(config => validateConfig(config)),
map(config => config.port)
)

See

  • map - for transforming values without nesting
  • flatten - for flattening nested Results