nomisaur / safely

safely catch errors from promises or functions, asynchronously or synchronously

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

safely

npm (scoped)

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.

About

safely catch errors from promises or functions, asynchronously or synchronously

License:MIT License


Languages

Language:JavaScript 100.0%