try-catch-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

try-catch-js

Lots of packages implement similar inline approach to handling errors in JavaScript without writing try/catch blocks. This one is a bit different because it follows proposal-try-expression.

Install

npm install try-catch-js --save

Usage

Asynchronously

tryAsync accepts either a Promise object or an async function that will be executed in order to get a promise. Resolved or rejected value is stored under value property, but if the promise is rejected caught property will be set to true.

If an async function is given then additional arguments to pass are supported.

Example
import { tryAsync } from 'try-catch-js';

interface User {
  id: string
  name: string
}

async function getUser(id: string): Promise<User> {
  if (id !== '1') {
    throw new Error('User was not found');
  }
  
  return { id: '1', name: 'John' };
}

async function asyncExample(): Promise<void> {
  const result = await tryAsync<User, Error>(getUser('1')); // as a promise
  // or const result = await tryAsync<User, Error>(getUser, '1'); // as an async func
  
  if (result.caught) {
    // Contains thrown/rejected value
    return console.dir(result.value);
  }
  
  // Contains user data
  console.dir(result.value);
}

Synchronously

Similar to tryAsync, but accepts only a regular function with optional parameters.

Example
import { trySync } from 'try-catch-js';

function divide(a: number, b: number): number {
  if (b === 0) {
    throw new Error('Cannot divide by zero!');
  }
  
  return a / b;
}

function syncExample(): void {
  const result = trySync<number, Error>(divide, 10, 5);
  
  if (result.caught) {
    return console.dir(result.value);
  }

  console.dir(result.value);
}

Dependents (0)

Package Sidebar

Install

npm i try-catch-js

Weekly Downloads

15

Version

1.0.1

License

MIT

Unpacked Size

88.9 kB

Total Files

12

Last publish

Collaborators

  • pushys