Personally - I recommend using ajel instead. Follow this link to see the new api.
ajel-go is a 312 byte set of functions that encourage you to handle errors in a way that is similar to Golang.
yarn add ajel-go eslint-plugin-ajel-go
pnpm add ajel-go eslint-plugin-ajel-go
// Handling async functions that throw
import { ajel } from 'ajel-go';
async function main() {
const [result, err] = await ajel(Promise.resolve('hello world'));
if (err) {
return err;
}
return result;
}
// Handling synchronous functions that throw
import { sjel } from 'ajel-go';
function main() {
const [result, err] = sjel(JSON.parse)("{}");
if (err) {
return err;
}
return result;
}
ajel
and sjel
are a set of functions that return a tuple representing a potential result and a potential error.
On success, the result item has value. On error, the error item has value. It's that simple.
More interestingly, it comes with a series of linting tools to help enforce the paradigm available in the package eslint-plugin-ajel
{
plugins: ['ajel-go'],
extends: [
'plugin:ajel-go/recommended',
],
}
docs
: A placeholder documentation site powered by Next.jsajel
: The core library (function)eslint-plugin-ajel
: Eslint rules for proper usage and error handling paradigmbenchmarks
: Testing ajel-go's performance against other methods