errgo-ts
Version:
A lightweight error handling library inspired by Go and Rust.
20 lines (19 loc) • 454 B
JavaScript
import { coerceError } from "./coerce-error.js";
export function tryCatch(action) {
try {
const result = action();
if (result instanceof Promise) {
return result.then((v) => ({
val: v,
}), (e) => ({
err: coerceError(e),
}));
}
else {
return { val: result };
}
}
catch (e) {
return { err: coerceError(e) };
}
}