JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1427
  • Score
    100M100P100Q104147F
  • License MIT

Clean up code by removing try-catch-finally blocks.

Package Exports

  • no-try

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (no-try) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

🚀 No-Try 🚀

Clean up your code base by removing those ugly try-catch-finally blocks!


😍 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. 🤘🤘

no-try now supports both Object Destructuring, as well as Array Destructuring, allowing for more flexible naming of your variables.

🔧 Installation

npm install --save no-try

🎸 Usage

To get up and running quickly, simply install the package:

npm install no-try

Then use it in your code:

Object Destructuring:

JS:

const noTry = require("no-try").noTry;
const noTryAsync = require("no-try").noTryAsync;

// Without a custom error handler
const { result, error } = noTry(() => myThrowableMethod());

// With a custom error handler
const { result, error } = noTry(
  () => myThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Handle methods that return a Promise without a custom error handler
const { result, error } = await noTryAsync(() => myAsyncThrowableMethod());

// Handle methods that return a Promise with a custom error handler
const { result, error } = await noTryAsync(
  () => myAsyncThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Use result
if (error) {
  // Show error alert
}

sendMyResultToMethod(result);

TS:

import { noTry, noTryAsync } from "no-try";

// Without a custom error handler
const { result, error } = noTry(() => myThrowableMethod());

// With a custom error handler
const { result, error } = noTry(
  () => myThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Handle methods that return a Promise without a custom error handler
const { result, error } = await noTryAsync(() => myAsyncThrowableMethod());

// Handle methods that return a Promise with a custom error handler
const { result, error } = await noTryAsync(
  () => myAsyncThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Use result
if (error) {
  // Show error alert
}

sendMyResultToMethod(result);

Array Destructuring (Note: the /tuple in the import path is required):

JS:

const noTry = require("no-try/tuple").noTry;
const noTryAsync = require("no-try/tuple").noTryAsync;

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

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

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

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

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

sendMyResultToMethod(result);

TS:

import { noTry, noTryAsync } from "no-try/tuple";

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

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

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

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

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

sendMyResultToMethod(result);