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

@meetsnowmaker/trycatcher

v0.1.0

Published

Graceful failure handler for synchronous functions.

Downloads

36

Readme

@meetsnowmaker/trycatcher

npm (scoped) npm bundle size (scoped)

Embrace failure in synchronous functions.

Hello 0.1.x

The default export is set in stone but further flavors are coming.

Introduction

Ever since I started learning about buzzwords like functional programming and asynchronous functions in node, the old way of trying and catching errors felt off. To avoid making every function asynchronous just to be able to properly return the desired outcome to the const I defined, I made a simple hacky attempt to get a predetermined response from the tryable function.

As I naturally progressed to urge for maximum immutability, it felt somewhat more convenient to define both outcomes with a single call. After a while I noticed this simple function had numerous copies in almost every project I worked on. I was thinking it might be a great opportunity to learn more about the npm packaging lifecycle, so here it is. My first dummy npm project. Bye let you might be missed.

Please note: this is definitely not an original idea, just my take on a solution.

How to

The default version returns with an array where index 0 is always either null or the desired outcome, and index 1 is null or the caught error. When defining you can destruct the array and get properly named variables for the predetermined indexes. I prefer using arrow functions for the argument. You can express more complex, like multi-line or multi-step operations inside the curly braces, just dont forget to use a return on the end. See examples for more use cases. I might add useful ones eventually.

import tryCatcher from '@meetsnowmaker/trycatcher';

const shouldWork = JSON.stringify(['asd', 'kek']);
const shouldFail = '["asd",kek]';

const [parsed, error] = tryCatcher(() => JSON.parse(shouldWork));

if (error) {
  throw error;
}

console.log('parsed', parsed);

const [maybeParsed, definitelyError] = tryCatcher(() => JSON.parse(shouldFail));

if (definitelyError) {
  throw definitelyError;
}

console.log('maybeParsed', maybeParsed);

will yield

parsed [ 'asd', 'kek' ]
C:\...\example.js:42
  throw definitelyError;
  ^

SyntaxError: Unexpected token k in JSON at position 7
    at JSON.parse (<anonymous>)

Roadmap

Eventually I should implement proper type support and more variables of the base function.

Things I want to add in the future:

  • typescript
  • object like return with undefined instead of null, so one can inject default value when destructuring
  • webpack for minifying
  • some real life examples
  • some real life test scenarios with BDD
  • GitHub Actions for publishing