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 🙏

© 2026 – Pkg Stats / Ryan Hefner

safethen

v1.0.9

Published

Functional wrappers for try/catch

Downloads

1

Readme

safethen

Function wrappers for synchronous and asynchronous try/catch.

Motivation

This tiny package provides two functions that let you write less error checking code. Useful for handling MongoDB documents, DOM nodes etc.

const { safe, safeThen } = require('safethen');

safe() - use in synchronous code

const { safe } = require('safethen');

// a random nested objects
const obj = { a: { } };

// if undefined is an accepted value
// valueOrUndefined === undefined
const valueOrUndefined = safe(_ => obj.a.b.c);

// if you don't want undefined
// valueOrUndefined === 'some default'
const valueOrDefault = safe(_ => obj.a.b.c, 'some default');

// wrap any logic in safe()
// valueOrUndefined === 'some default'
const valueOrDefault = safe(_ => 1 < 2 && obj.a.b.c, 'some default');


// if-like conditions in promise chains
Promise.resolve(obj)
    .then(x => safe(_ => x.a.b.c, 'phew!'))
    .then(console.log); // logs 'phew!'

// vanila promises version
Promise.resolve(obj)
    .then(x => Promise.resolve()
        // this then/catch nesting is necessary, as don't want to
        // swallow outer errors
        .then(_ => x.a.b.c)
        .catch(_ => 'phew!')
    )
    .then(console.log); // logs 'phew!'

safeThen() - use with promises

const { safeThen } = require('safethen');

const obj = { a: { b: { c: true }} };

// value === 123
safeThen(_ => obj.x.y, 123).then(value => ...);

const ajaxRequest = _ => Promise.resolve('OK');

// value === 'OK'
safeThen(_ => obj.a.b.c && ajaxRequest(), 123)
    .then(value => ...);

// value === 123, ajaxRequest() is never called
safeThen(_ => obj.ohNoes() && ajaxRequest(), 123)
    .then(value => ...);


// value === undefined
safeThen(_ => blowUp())
    .then(value => console.log(value === undefined))   // true
    .catch(err => console.log('this is never logged');

Source

/**
 * safe
 *
 * @param {function} syncFunc       function exectued inside try/catch block
 * @param {any}      [defaultValue]
 * @returns {any}    result of `syncFunc()` call or `defaultValue`
 */
function safe(syncFunc, defaultValue) {
    try {
        const value = syncFunc();
        return value !== void 0 ? value : defaultValue;
    } catch (ex) {
        return defaultValue;
    }
}

/**
 * safeThen
 *
 * @param {function}  asyncFunc      function executed in a Promise context
 * @param {any}       [defaultValue]
 * @returns {Promise} resolved with the `asyncFunc()` result or `defaultValue`
 */
function safeThen(asyncFunc, defaultValue) {
    return Promise.resolve()
        .then(asyncFunc)
        .then(function (value) { return value !== void 0 ? value : defaultValue; })
        .catch(function () { return defaultValue; });
}