- Build with Typescript, ship with Built-in TypeDefinitions
- Handle
Promise
and async Function - Zero dependencies
Just use npm to install the library, bring with built-in type definitions:
npm i try2be
Convert a try-catch style code to result and error in array (Go-style, Best with array destruction)
import { try2Arr } from "try2be";
const [ret, err] = try2Arr(() => {
return "tom";
});
// ret = 1;
const [ret, err] = try2Arr(() => {
throw "jerry";
});
// err = 'jerry';
import { try2Arr } from "try2Arr";
const [ret, err] = try2Arr(() => {
return "tom";
});
// ret = 1;
const [ret, err] = try2Arr(() => {
throw "jerry";
});
// err = 'jerry';
Convert a try-catch style code to result and error in object (Best with object desctruction)
import { try2Obj } from "try2be";
const { ret, err } = try2Obj(() => {
return "tom";
});
// ret = 1;
const { ret, err } = try2Obj(() => {
throw "jerry";
});
// err = 'jerry';
import { try2Arr } from "try2Obj";
const { ret, err } = try2Obj(() => {
return "tom";
});
// ret = 1;
const { ret, err } = try2Obj(() => {
throw "jerry";
});
// err = 'jerry';
Convert a try-catch code to just ignore throw error
import { try2Ignore } from 'try2Be'
const res = try2Ignore(() => {
return "tom";
})
// res = "tom";
const res = try2Ignore(() => {
return "jerry";
})
// res = undefined;
import { try2Arr } from "try2Arr";
const [ret, err] = await try2Arr(async () => {
return "tom";
}) // Type: Promise<[string, any]>;
// ret = "tom";
const [ret, err] = await try2Arr(async () => {
throw "jerry";
}); // Type: Promise<[undefined, any]>;
// err = "jerry";
const { try2Arr } = require('try2Arr');