dd-try-catch
v1.1.2
Published
Simple Try Catch with only one return
Downloads
3
Maintainers
Readme
dd-try-catch
Simple Try Catch with only one return
Installation
$ npm install dd-try-catch
Use
// CommonJS
const { tryCatch, tryCatchAsync } = require('dd-try-catch');
// ES6
import { tryCatch, tryCatchAsync } from 'dd-try-catch';
// Synchronous
const [error, result] = tryCatch(<Function>, <Param>);
// Asynchronous (Async / Await)
const [error, result] = await tryCatchAsync(<Function>, <Param>);
Synchronous examples
const { parse } = JSON;
const [error, result] = tryCatch(parse, '{"test": "ok"}');
// or
const fn = (param) => JSON.parse(param);
const [error, result] = tryCatch(fn, '{"test": "ok"}');
// or
const fn = () => JSON.parse('{"test": "ok"}');
const [error, result] = tryCatch(fn);
// or
const param = '{"test": "ok"}';
const fn = () => JSON.parse(param);
const [error, result] = tryCatch(fn);
// Console on return
if (error) console.log(error.message);
if (!error) console.log(result.test);
// error = null or {"message": "Error message"}
// result = null or {"test": "ok"}
Asynchronous examples (Async / Await)
(async () => {
const axios = require('axios');
const fn = async () => await axios.get('https://myapi.com/posts'); // Returns a promise
const [error, result] = await tryCatchAsync(fn); // Execute and resolve a promise
// or
const fn = () => axios.get('https://myapi.com/users'); // Returns a promise
const [error, result] = await tryCatchAsync(fn); // Execute and resolve a promise
console.log(result.data);
})();