Rust-like Result
and ResultAsync
for JavaScript.
Result
is a type that represents either success (Ok
) or failure
(Err
), ResultAsync
is the asynchronous version of Result
.
> npm install @rustresult/result
> pnpm install @rustresult/result
> yarn add @rustresult/result
This package implements a Rust-like Result
, nearly all methods are similar to the Rust Result.
const ok = Ok(1);
const err = Err('Some error message');
import fs, { Stats } from 'node:fs/promises';
const result1: Result<Stats, Error> = await fs
.stat(path)
.then((value) => Ok(value))
.catch((err) => Err(err));
const readdir = resultifyAsync<Error>(fs.readdir);
const result2 = readdir(path, { withFileTypes: true });
// ^ ResultAsync<Dirent[], Error>
Check out @rustresult/result
for the core of Rust-like Result
- Check out
@rustresult/json
for the simple JSON (de)serialization support - Check out
@rustresult/json-serializr
for the better JSON (de)serialization support using theserializr
library
Result
is not a built-in feature in JavaScript world. You will definitely feel that it's hard to make Result
work with third-party libraries. There are also some helpers for you.
- Check out
@rustresult/typeorm
for helpers that can help you use thetypeorm
library
This project doesn't implement Rust-like Option
. Handling undefined
/null
is not as hard as it was a few years ago, because right now we already have these proposals to help handle it:
The rustlike-result
project is available as open source under the terms of the MIT license.