fck-trycatch
v1.0.5
Published
wrap a func in a try/catch block and get whatever is returned plus error as value
Downloads
323
Readme
fck-trycatch
Description
Tiny module that exports two functions: attempt
and attemptSync
. These functions accept a function as first argument and call it wrapped in a try/catch block, returning an error (if any, else null) and whatever was returned by the provided function (if anything, else null).
Usage
attempt
/attemptSync
accept the following parameters:
func
: the function to be wrapped in try/catch.thisArg
(optional): iffunc
is a method this argument should be used to bind it to the proper context....args
(optional, depending onfunc
): the arguments to be passed to the function call.
Example
import { attempt } from "fck-trycatch";
const main = async () => {
const [fetchErr, res] = await attempt(fetch, null, "/someurl");
if (fetchErr) {
console.error(fetchErr);
return;
}
const [parseErr, data] = await attempt(res.json, res);
if (parseErr) {
console.error(parseErr);
return;
}
console.log(data);
};
main();