A TypeScript port of Rust's Result<T>
and Option<T>
structs
You can use the following command to install this package, or replace pnpm add
with your package manager of choice.
pnpm add @flowr/result
Note: While this section uses import
, the imports match 1:1 with CJS imports. For example const { Result } = require('@flowr/result')
equals import { Result } from '@flowr/result'
.
Old code without Result.from
:
try {
const returnCode = someFunctionThatMightThrow();
return returnCode;
}
catch (error) {
console.error(error);
return null;
}
New Code with Result.from
:
import { Result } from '@flowr/result';
const returnCode = Result.from(() => someFunctionThatMightThrow());
return returnCode.unwrapOrElse((error) => {
console.error(error);
return null;
});
Old code without Result.fromAsync
:
try {
const returnCode = await someFunctionThatReturnsAPromiseAndMightReject();
return returnCode;
}
catch (error) {
console.error(error);
return null;
}
New Code with Result.fromAsync
:
import { Result } from '@flowr/result';
const returnCode = await Result.fromAsync(async () => someFunctionThatReturnsAPromiseAndMightReject());
return returnCode.unwrapOrElse((error) => {
console.error(error);
return null;
});