@aicacia/trycatch
v0.1.1
Published
typesafe go like tuple results for async and sync code
Downloads
47
Maintainers
Readme
ts-trycatch
typesafe go like tuple results for async and sync code
Examples
Sync
import { trycatch } from "@aicacia/trycatch";
const [numbers, err] = trycatch(() => {
const numbers = [];
for (let i = 0; i < 10; i++) {
numbers.push(maybeWillThrow(i));
}
return numbers;
});
if (err) {
// handle err
}
// use numbersAsync
import { trycatch } from "@aicacia/trycatch";
const [numbers, err] = await trycatch(async () => {
const numbers = [];
for (let i = 0; i < 10; i++) {
numbers.push(await maybeWillThrowAsync(i));
}
return numbers;
});
if (err) {
// handle err
}
// use numbers