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

dotry

v1.4.1

Published

Catch errors without try...catch...

Downloads

21

Readme

dotry

Catch errors without try...catch...

Tired of writing too much try {} catch {}, try this funny tool, it wraps the error and the return value into a two-item array, which we can extract like this const [err, res] = _try(...).

And this function supports all JavaScript functions, such as Function, AsyncFunction, GeneratorFunction and AsyncGeneratorFunction, and Promise instances, regardless of transpiling to es5, es2015, es2016 or other versions.

Warning: the main function of this package has been merged into @ayonli/jsext, this package will stay for a while, and will be deprecated in the near future.

Install

npm i dotry

Example

Regular Function

import _try from "dotry";

function check(str: string) {
    if (somethingWentWrong()) {
        throw new Error("something went wrong");
    }

    return str;
}

let [err, res] = _try(check, "Hello, World!");

// Or simply
let [err, res] = _try(() => {
    if (somethingWentWrong()) {
        throw new Error("something went wrong");
    }

    return "Hello, World!";
});

Async Function

import _try from "dotry";

async function checkAsync(str: string) {
    if (await somethingWentWrong()) {
        throw new Error("something went wrong");
    }

    return await Promise.resolve(str);
}

let [err, res] = await _try(checkAsync, "Hello, World!");

// Or
let [err, res] = await _try(checkAsync("Hello, World!"));

// Or simply
let [err, res] = await _try(async () => {
    if (await somethingWentWrong()) {
        throw new Error("something went wrong");
    }

    return "Hello, World!";
});

Generator Function

import _try from "dotry";

function* iterate(...data: number[]) {
    for (let num of data) {
        if (num > 9) {
            throw new RangeError(`number ${num} is out of range`);
        } else {
            yield num;
        }
    }
}

for (let [err, value] of _try(iterate, 1, 2, 3, 4)) {
    // ...
}

// Or
for (let [err, value] of _try(iterate(1, 2, 3, 4))) {
    // ...
}

Async Generator Function

import _try from "dotry";

async function* iterateAsync(...data: number[]) {
    for (let num of data) {
        if (num > 9) {
            throw new RangeError(`number ${num} is out of range`);
        } else {
            yield num;
        }
    }
}

for await (let [err, value] of _try(iterateAsync, 1, 2, 3, 4)) {
    // ...
}

// Or
for await (let [err, value] of _try(iterateAsync(1, 2, 3, 4))) {
    // ...
}

Happy Coding!