1.1.2 • Published 3 years ago

dd-try-catch v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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);
  
})();
1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.0

3 years ago