@emberscodes/safely
v2.0.1
Published
safely catch errors from promises or functions, asynchronously or synchronously
Downloads
3
Maintainers
Readme
safely
errors bad. catch errors. safely makes everything safe.
Install
$ npm install @emberscodes/safely
Usage
safely doesnt need to be awaited for syncronous functions:
// pass safely a syncronous function!
const [result, error] = safely(() => {
throw "bonk";
});
// result === undefined
// error === 'bonk'
safely can handle async functions too:
// pass an asyncronous function and safely await it!!
const [result, error] = await safely(async () => {
throw "bonk";
});
// result === undefined
// error === 'bonk'
safely can also take promises:
const bonkPromise = fetch("bonk.com");
// pass a promise and safely await that too!!!
const [result, error] = await safely(bonkPromise);
// result === 'you have been bonked'
// error === undefined
safely is very useful:
const [[bonkResult, bonkError], [images, imagesError]] = await Promise.all([
safely(fetch("bonk.com")),
safely(fetch("bonk.com/images")),
]);
pass anything to safely:
const [res, err] = safely("anything");
// res === 'anything'
Propoganda
Safely is very safe. Safely handles anything. You can pass anything to safely. Wrap everything in safely. Safely will fix you code. Safely will cure your depression.