4.0.0 • Published 19 days ago

no-try v4.0.0

Weekly downloads
565
License
MIT
Repository
github
Last release
19 days ago

😍 About

Working in a code base where you can expect methods to throw can lead to situations where your logic is wrapped in try-catch blocks. It also leads to other code design problems. 🤢

no-try tackles this by removing the try-catch to an external method, whilst allowing the flexibility of handling the error thrown appropriately and getting access to the return value of the method that will throw. 🤘🤘

🔧 Installation

npm install --save no-try

🎸 Usage

First we need to set up our import

JavaScript (all)

const useTry = require("no-try").useTry;
const useTryAsync = require("no-try").useTryAsync;

TypeScript or ES6+

import { useTry, useTryAsync } from "no-try";

Now let's use it

// Without a custom error handler
const [error, result] = useTry(() => myThrowableMethod());

// With a custom error handler
const [err, res] = useTry(
  () => myThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Handle methods that return a Promise without a custom error handler
const [err2, res2] = await useTryAsync(() => myAsyncThrowableMethod());

// Handle methods that return a Promise with a custom error handler
const [err3, res3] = await useTryAsync(
  () => myAsyncThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Use result
if (error || err || err2 || err3) {
  // Show error alert
}

sendMyResultToMethod(result);
3.2.0

19 days ago

4.0.0

19 days ago

3.1.0

4 years ago

2.1.1

4 years ago

3.0.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago