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

make-try

v1.0.0

Published

``` npm i ts-try-catch-wrap ```

Downloads

1

Readme

Usage

npm i ts-try-catch-wrap

Why

test function

import { makeTry } from "ts-try-catch-wrap";

// test function
const getSyncFile = (filename: string) => {
  if (Math.random() < 0.5) {
    throw new Error("fail");
  }

  return filename;
};

referencing variable repeatedly

Bad - referencing variable repeatedly

(() => {
  let file; // first show file variable

  try {
    // second show file variable
    file = getSyncFile("my-file-name");
  } catch (err: unknown) {
    console.log("error!");
    return;
    // handle err
  }

  // using file variable
  console.log(file);
})();

Nice - assigned only once

(() => {
  const tryGetFileName = makeTry(getSyncFile);

  // first show file variable;
  const { err, hasError, result: file } = tryGetFileName("my-file-name");

  if (hasError) {
    console.log("error!");
    return;
    // handle err
  }

  // using file variable
  console.log(file);
})();

try catch hell

Bad - try catch hell

let file1;
let file2; // after assiging the file, file2 can be assigned.

try {
  // second show file variable
  file1 = getSyncFile("file1");
  try {
    file2 = getSyncFile("file2");
  } catch (err: unknown) {
    console.log("file2 error!");
    return;
    // handle err;
  }
} catch (err: unknown) {
  console.log("file1 error!");
  return;
  // handle err
}

// using variables
console.log(file1);
console.log(file2);

Nice - remove try catch hell

(() => {
  const getTrySyncFile = makeTry(getSyncFile);

  const {
    result: file1,
    hasError: file1HasErr,
    err: file1Err,
  } = getTrySyncFile("file1");

  if (file1HasErr) {
    console.log("file1 error!");
    return;
    // handle err
  }

  const {
    result: file2,
    hasError: file2HasErr,
    err: file2Err,
  } = getTrySyncFile("file2");

  if (file2HasErr) {
    console.log("file1 error!");
    return;
    // handle err
  }

  // using variables
  console.log(file1);
  console.log(file2);
})();

So what about asynchronous?

just using makeTry

const getAsyncFile = async (filename: string) => {
    await new Promise(res => setTimeout(res, 10));
    return filename;
}

(async () => {
    const tryGetFileName = makeTry(getAsyncFile);

    const { 
        hasError: file1hasError, 
        result: file1 
    } = await tryGetFileName('async-my-file-name-1');

    const { 
        hasError: file2hasError, 
        result: file2 
    } = await tryGetFileName('async-my-file-name-2');

    if (file1hasError || file2hasError) {
        return;
    }

    console.log(file1);
    console.log(file2);
})();

Abort

  const slowApi = async () => {
    await new Promise(res => setTimeout(res, 3000));
    return 'hi'
  }
  
  const trySlowApi = makeTry(slowApi, {
    abort: true,
    reason: 'slow api'
  });
  
  trySlowApi().then(console.log);
  
  setTimeout(trySlowApi.abort, 1500);