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

maybe-try

v1.0.12

Published

A declarative sync & async js maybe that notes something may fail

Downloads

9

Readme

maybe-try :see_no_evil:

A declarative sync & async js maybe that notes something may fail

Node and Browser Compatible

View on NPM

npm install maybe-try

Usage

The module exposes 3 methods, one for synchronous operations, one for promises, and one for callbacks.

Each method takes 2 parameters, the fallbackValue for error scenarios, and a function.

Both parameters are REQUIRED, maybe-try does not supply a default value for you

Returns an object with caught errors (if any) and either the result or fallbackValue, depending on success

More in-depth examples can be found here

Synchronous Version

const maybeTry = require('maybe-try');

// Synchronous Version

const data = 'some string';
const { error, result } = maybeTry.catch(false, functionThatOperatesOnDataAsIfItWereAnArray(data));
// No need to run try/catches in your code blocks, maybeTry resolves the caught error and result with assignment

Promise Version

// Promise Version

function fetchReviewsForProduct(productId) {
  const fallbackValue = [{ rating: 5 }];

  return maybeTry.promise(fallbackValue, getReviewsPromise)
    .then(({ error, result }) => {
      // No need to handle catches here, maybeTry resolves both the db error and our fallback value
      // Decide what you'd like to do with the error from here, either ignore and use the fallback value, or handle it manually
      // However, you still have access to the error in the response body should you need it

      // Access both the result and the error on response
      console.log('Promise Response', result);
      return result; // fallbackValue ([{ rating: 5 }])
    });
}

Callback Version

// Callback Version

const cbFallbackValue = [{ name: 'Tom' }];
fetchFriendsForUser(1, maybeTry.callback(cbFallbackValue, (err, { error, result }) => {
  // No need to handle errors here, maybeTry resolves both the db error and our fallback value
  // The first argument (err) will always be null to keep with error-first callback patterns
  // However, you still have access to the error in the response body should you need it

  // Decide what you'd like to do with the error from here, either ignore and use the fallback value, or handle it manually
  // Access both the result and the error on response
  console.log('Callback Response', result);
  return result; // fallbackValue (everything's ok, at least we have Tom)
}));

Registering an errorHandler

An optional feature to register a function to handle errors for all occurances within the module

It's useful if you'd like to only stick to the happy path with your application logic but send logs to an external service when errors do occur

It should be noted that the errorHandler is a fire and forget method. You cannot chain to it or call it explicitly.

Example using the Syncronous Version

const errorHandler = error => {
  const { message } = error;
  console.log('An Error was caught from maybe-try', error);
  logErrorsToAnExternalService(error);
};

maybeTry.registerErrorHandler(errorHandler);

// The API remains the same, but the errorHandler will be firing in the background
const data = 'some string';
const { error, result } = maybeTry.catch(false, functionThatOperatesOnDataAsIfItWereAnArray(data));
// at this point, logErrorsToAnExternalService() has been called