Skip to main content
This guide will teach you the fundamentals of NeverThrow through practical examples.

Basic Result types

Every Result is either Ok (success) or Err (failure). Create them using the ok() and err() functions:
The Result type is defined as: type Result<T, E> = Ok<T, E> | Err<T, E> where T is the success value type and E is the error type.

Your first Result-returning function

Instead of throwing errors, return a Result that makes success and failure explicit:
Key insight: With Result, errors become part of your function’s type signature, making them impossible to ignore.

Transforming Results with map

Use .map() to transform the success value while leaving errors untouched:

Chaining with andThen

Use .andThen() when your next operation might also fail:
1

Define functions that return Results

2

Chain them with andThen

3

Short-circuits on first error

map vs andThen: Use map when your transformation cannot fail. Use andThen when your transformation returns another Result.

Handling both cases with match

Use .match() to handle both success and error cases and return a final value:
Alternatively, use .unwrapOr() to provide a default value for errors:

Working with async code

For asynchronous operations, use ResultAsync which behaves like a Promise but with Result methods:
ResultAsync is thenable, so you can use it with await or .then() just like a regular Promise.

Combining multiple Results

Use Result.combine() to aggregate multiple Results into a single Result:
To collect all errors instead of short-circuiting, use Result.combineWithAllErrors():

Next steps

You now know the basics of NeverThrow. Here’s what to explore next:

Core concepts

Learn about the Result type in depth

Async operations

Master ResultAsync for promises

Error recovery

Handle and recover from errors with orElse

API reference

Browse all available methods
Remember: Never use ._unsafeUnwrap() in production code. It should only be used in test environments. Use .match() or .unwrapOr() instead.