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

@jfdi/attempt

v1.3.9

Published

Functional error handling implementations for sync & async, to replace try/catch forever

Downloads

184

Readme

attempt

The sane way to do error handling in a Functional Programming inspired way

Banish the ungainly, scope-bound try/catch/finally forever!

This is a very simple, lightweight library to implement functional try/catch everywhere. I found myself copying the code into every single project I wrote, so I decided to make it a proper library. It has no dependencies. It's built for both browser and nodeJs use.

Breaking Changes since v1.2.x

Removed attemptAllPromise because it always caused confusion. I might replace it in future with something better.

Installation

npm i @jfdi/attempt

Usage

For synchronous code...

const [error, result] = attempt(fn);
// insert handling code here

For async code...

const [error, result] = await attemptPromise(fn);
// insert handling code here

or for old style promise handling...

attemptPromise(fn).then(([error, result]) => {
    // insert handling code here
});

Examples?

See the tests. They're pretty comprehensive.

Why?

Because I've always hated the ugliness of the try/catch/finally construct ever since I first encountered it in C++.

It's not just an aesthetic thing, although it does make code ugly.

It's mostly this:

let success; // a variable ripe for mutation... can't define this inside any scope below or it'll be unavailable in the others

try {
    // Here's one lexical scope...
    // The code to attempt
    success = true; // mutation
} catch (e) {
    // Here's another lexical scope...
    // The code to handle the error
    success = false; // mutation
} finally {
    // and just because we love all these isolated lexical scopes...
    // Here's the code to run after the whole sorry mess above
    if (success) console.log("Yay!");
}

... and ne'er the three lexical scopes shall meet. Any variable created in the try scope won't be available in the catch scope, and so on, leading to scoping compromises (like declaring variables using let before the try and mutating them in later scopes) and nasty, messy, hard-to-follow code with masses of boilerplate that goes on and on for pages whilst actually doing very little. Yuk. The solution is attempt or attemptPromise. Voila. You're welcome.