Package Exports
- @freelight-software/dont-throw
- @freelight-software/dont-throw/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@freelight-software/dont-throw) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@freelight-software/dont-throw
A tiny TypeScript library for safe, result-based function returns that prevent exceptions from being thrown. Inspired by Rust's Result type, this library helps you handle errors without using try/catch.
Installation
npm install @freelight-software/dont-throwUsage
Wrapping Functions
Use dontThrow to wrap any function that might throw. Instead of throwing, it returns a Result object.
import { dontThrow, ok, err, type Result } from '@freelight-software/dont-throw';
const mightThrow = (x: number) => {
if (x < 0) throw 'Negative!';
return x * 2;
};
const safeFn = dontThrow(mightThrow);
const result = safeFn(-1);
if (result.isOk()) {
console.log('Value:', result.value);
} else {
console.error('Error:', result.error);
}Creating Results Directly
const success = ok(123);
const failure = err('Something went wrong');
if (success.isOk()) {
// access success.value
}
if (failure.isErr()) {
// access failure.error
}Unwrapping
result.unwrap()returns the value ifOk, or throws the error ifErr.
Async Support
Use dontThrowAsync for async functions:
import { dontThrowAsync } from '@freelight-software/dont-throw';
const asyncFn = async (x: number) => {
if (x < 0) throw new Error('Negative!');
return x * 2;
};
const safeAsync = dontThrowAsync(asyncFn);
const result = await safeAsync(1);
if (result.isOk()) {
// result.value
}API
ok(value)— Create a success result.err(error)— Create an error result.dontThrow(fn)— Wrap a sync function to return aResult.dontThrowAsync(fn)— Wrap an async function to return aPromise<Result>.Result.isOk()/Result.isErr()Result.value/Result.errorResult.unwrap()Result.map(fn)Result.andThen(fn)
License
ISC