npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

try-catch-expression

v1.0.8

Published

try-catch-expression provides try..catch as expression.

Downloads

77

Readme

try-catch-expression

try-catch-expression provides try..catch as expression.

Status

| Category | Status | | ---------------- | --------------------------------------------------------------------------------------------------- | | Version | npm | | Dependencies | David | | Dev dependencies | David | | Build | GitHub Actions | | License | GitHub |

Installation

$ npm install try-catch-expression

Rationale

For handling errors, JavaScript and TypeScript provide the try / catch statement. While it is highly useful, it sometimes makes your code hard to read, e.g. if you want to assign a variable and have a fallback in the catch clause:

let result;

try {
  result = getValue();
} catch {
  result = getDefaultValue();
}

console.log(result);

For this (and other) scenarios it would be helpful it try / catch was also available as an expression, so that you could write your code in a more convenient way:

const result = try {
  return getValue();
} catch {
  return getDefaultValue();
}

console.log(result);

Unfortunately, neither JavaScript nor TypeScript support this. That's what this module adds.

Quick start

First you need to add a reference to try-catch-expression in your application:

const { tryCatch, tryFinally, tryCatchFinally } = require('try-catch-expression');

If you use TypeScript, use the following code instead:

import { tryCatch, tryFinally, tryCatchFinally } from 'try-catch-expression';

To use a simple try / catch, use the tryCatch function and pass a potentially failing function as well as a function that acts as a catch clause. In that function, you may return a value that is then also returned by the surrounding tryCatch function, or re-throw.

const result = tryCatch(
  () => {
    // Try something...
  },
  ex => {
    // Return a default value or re-throw the exception.
  }
);

This means that the variable result will then either contain the return value of the try function, in case it succeeds. If it fails, the value from the catch function is returned. Any error thrown in the catch function will be passed through to the enclosing scope.

Using finally

Besides tryCatch, there are also tryCatchFinally and tryFinally available:

const result = tryCatchFinally(
  () => {
    // Try something...
  },
  ex => {
    // Return a default value or re-throw the exception.
  },
  () => {
    // Clean up.
  }
);

const result = tryFinally(
  () => {
    // Try something...
  },
  () => {
    // Clean up.
  }
);

Note that the return value of the finally function will be discarded, to avoid confusing behavior.

Running quality assurance

To run quality assurance for this module use roboter:

$ npx roboter