Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

callback-wrappers

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callback-wrappers

wrappers for async callbacks that implement common error handling scenarios

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 23 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc