Basic Result types
EveryResult 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 aResult that makes success and failure explicit:
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:
.unwrapOr() to provide a default value for errors:
Working with async code
For asynchronous operations, useResultAsync which behaves like a Promise but with Result methods:
Combining multiple Results
UseResult.combine() to aggregate multiple Results into a single Result:
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