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-with

v0.2.0

Published

[![Build Status](https://travis-ci.org/levp/try-with.svg?branch=master)](https://travis-ci.org/levp/try-with)

Downloads

9

Readme

Build Status

try-with

A utility for safe and convenient handling of managed resources in JavasScript.

The purpose of this utility is to provide a convenient and safe way of using resources that must be manually released, such as remote connections and file handlers.

Similar concepts:

Installing

npm: npm i try-with
yarn: yarn add try-with

Example

A potential resource leak due to stream not being closed:

const stream = fs.createWriteStream(filePath);
// Next line will throw if `data` is not one of the allowed types.
stream.write(data);
// Will never be called if previous line threw and someone caught the error.
stream.close();

A safe variant of the above code:

const stream = fs.createWriteStream(filePath);
try {
  stream.write(data);
} finally {
  stream.close();
}

Functionally equivalent to the code above but using try-with:

const tryWith = require('try-with');

tryWith(fs.createWriteStream(filePath), stream => {
  stream.write(data);
});

API

tryWith(object, action, [cleanupMethodOrName])

  1. Invokes action, passing object to it as a parameter.
  2. After action is finished try to perform cleanup:
    1. If third argument was omitted:
      1. If dispose property exists on object, try to invoke it.
      2. Else if close property exists on object, try to invoke it.
    2. Else If third argument was provided and is a function, invoke it and pass object to it as a paramter.
    3. Else (third argument was provided but is not a function):
      1. If it is a string or a symbol, try to invoke a property with that key on object.
      2. Else convert third argument to string and try to invoke a property with that key on object.
  3. If an error was thrown when action was invoked, rethrow it.

Building and Testing

npm run build or npm run watch to compile TypeScript.
npm test to run tests, make sure to compile before doing so.

License

ISC