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

callback-wrappers

v0.3.0

Published

wrappers for async callbacks that implement common error handling scenarios

Downloads

4

Readme

callback-wrappers

Function wrappers for async callbacks that implement common, simple error handling scenarios.

Install

    npm install callback-wrappers

Description

Most async methods in the node world expect a callback with an (error, data) signature. In programming scenarios where complex error handling is impossible or unneccessary (for example you can simply log the error and exit the process) this can generate a lot of repetitive, boilerplate, error-handling code that can obscure your real logic, e.g.

asyncFunction1({ ... }, function (error, data) {
  if (error) {
    console.error(error);
    process.exit(1);
  } else {
    // some real logic here
    asyncFunction2({ ... }, function (error, data) {
      if (error) {
        console.error(error);
        process.exit(2);
      } else {
        // some more real logic here
      }
    });
  }
});

This module provides a bunch of wrappers that take a function with just (data) signature and produce a function with the (error, data) signature and the boiler plate logic in place. For example the exitIfError wrapper has the exact logic shown above, allowing for us to collapse that example down to

var exitIfError = require("callback-wrappers").exitIfError;

asyncFunction1({ ... }, exitIfError(1, function (data) {
  // some real logic here
  asyncFunction2({ ... }, exitIfError(2, function (data) {
    // some more real logic here
  });
});

There's also a nextIfError wrapper that takes a function with a (data, next) signature (where next is a callback of the (error, ...) variety). This simply passes error to next (and, unlike the other methods, does not log).

Function.prototype option

If messing with built-in objects' prototypes doesn't skeeve you out, you can use the Function() export to get Function.prototype decorated with all of the wrappers and our example can look like

require("callback-wrappers").Function();

asyncFunction1({ ... }, function (data) {
  // some real logic here
  asyncFunction2({ ... }, function (data) {
    // some more real logic here
  }.exitIfError(2));
}.exitIfError(1));

long & short wrapper names

The wrappers all follow a naming convention of actionIfError, where action is one of log, abort, exit, throw or next. For brevity these can be referenced by the initials l, a, x, t and n, followed by ie (for "If Error"). Note that in all cases the error is logged, and in no case, including logIfError, will the wrapped function be called if the error parameter isn't empty.

Change Log

  • 0.3.0: added nextIfError
  • 0.2.0: using Object.defineProperty to make the Function.prototype extensions not enumerable
  • 0.1.0: created

Acknowledgements

Thanks go to the nodejs group for comments and suggestions.

License

MIT