Skip to content

Function: match()

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): OkReturn | ErrReturn

Defined in: index.ts:699

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

NonPromise<R>

The Result to match on

onOk

(data) => NonPromise<OkReturn>

Function to handle Ok results

onErr

(error) => NonPromise<ErrReturn>

Function to handle Err results

Returns

OkReturn | ErrReturn

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): OkReturn | ErrReturn

Defined in: index.ts:704

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

NonPromise<R>

The Result to match on

onOk

(data) => NonPromise<OkReturn>

Function to handle Ok results

onErr

NonPromise<ErrReturn>

Function to handle Err results

Returns

OkReturn | ErrReturn

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): OkReturn | ErrReturn

Defined in: index.ts:709

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

NonPromise<R>

The Result to match on

onOk

NonPromise<OkReturn>

Function to handle Ok results

onErr

(error) => NonPromise<ErrReturn>

Function to handle Err results

Returns

OkReturn | ErrReturn

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): OkReturn | ErrReturn

Defined in: index.ts:714

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

NonPromise<R>

The Result to match on

onOk

NonPromise<OkReturn>

Function to handle Ok results

onErr

NonPromise<ErrReturn>

Function to handle Err results

Returns

OkReturn | ErrReturn

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): Promise<OkReturn | ErrReturn>

Defined in: index.ts:719

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

OrPromise<R>

The Result to match on

onOk

(data) => OrPromise<OkReturn>

Function to handle Ok results

onErr

(error) => OrPromise<ErrReturn>

Function to handle Err results

Returns

Promise<OkReturn | ErrReturn>

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): Promise<OkReturn | ErrReturn>

Defined in: index.ts:724

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

OrPromise<R>

The Result to match on

onOk

(data) => OrPromise<OkReturn>

Function to handle Ok results

onErr

OrPromise<ErrReturn>

Function to handle Err results

Returns

Promise<OkReturn | ErrReturn>

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): Promise<OkReturn | ErrReturn>

Defined in: index.ts:729

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

OrPromise<R>

The Result to match on

onOk

OrPromise<OkReturn>

Function to handle Ok results

onErr

(error) => OrPromise<ErrReturn>

Function to handle Err results

Returns

Promise<OkReturn | ErrReturn>

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"

Call Signature

match<R, OkReturn, ErrReturn>(result, onOk, onErr): Promise<OkReturn | ErrReturn>

Defined in: index.ts:734

Pattern matches on a Result, calling the appropriate handler function. Useful for unwrapping a Result taking both success and error cases into account.

Type Parameters

R

R extends Result<any, any>

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

result

OrPromise<R>

The Result to match on

onOk

OrPromise<OkReturn>

Function to handle Ok results

onErr

OrPromise<ErrReturn>

Function to handle Err results

Returns

Promise<OkReturn | ErrReturn>

The return value of the called handler function

Example

typescript
match(
  ok(42),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Success: 42"

match(
  err('error'),
  (data) => `Success: ${data}`,
  (error) => `Error: ${error}`
); // "Error: error"