A lib designed based on the philosophy of "Errors are values" (similar to Rust).
Using npm:
$ npm install @soulwallet/result
Using yarn:
$ yarn add @soulwallet/result
Using pnpm:
$ pnpm add @soulwallet/result
Once the package is installed, you can import the library using import
approach:
import { Result, Ok, Err } from '@soulwallet/result'
import { Result, Ok, Err } from '@soulwallet/result'
async function div(a: number, b: number): Promise<Result<number, Error>> {
if (b === 0) {
return new Err(new Error('Division by zero'));
}
return new Ok(a / b);
}
async function test() {
const result = await div(10, 2);
if (result.isErr()) {
console.log(`error: ${result.ERR.message}`);
} else {
console.log(`result: ${result.OK}`);
}
}
ISC