npm.io
0.0.0 • Published 7 years ago

@zhaoyao91/try-catch

Licence
MIT
Version
0.0.0
Deps
0
Size
3 kB
Vulns
0
Weekly
0

Try Catch

Return result and error instead of throwing.

Installation

npm i @zhaoyao91/try-catch

Usage

handle sync function
const tryCatch = require("@zhaoyao91/try-catch");

function fn(...args) {
  // throw or return
}

const [error, result] = tryCatch(fn, ...args);
handle async function
const tryCatch = require("@zhaoyao91/try-catch");

async function fn(...args) {
  // throw or return
}

const [error, result] = await tryCatch(fn, ...args);
Customize promise judge logic
require("@zhaoyao91/try-catch").isPromise = function(x) {
  // customized logic to tell if x is a promise
};

License

MIT

Keywords