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

fail-fast

v1.0.0

Published

A failfast module for idiomatic nodejs; splits error handling from successful continuation.

Downloads

81

Readme

#fail-fast Build Status

A failfast module for idiomatic nodejs splitting error handling from successful continuation.

I recognize that it is a matter of style, but I prefer to deal primarily with the success case and let errors either fall into error-handlers or crash the app. That said, I've used code similar to this failfast function in the past to split off errors and decided it was 'bout time I made it its own thing.

I'm tired of seeing:

      if (err) {
        return callback(err);
      }

polluting my boomerang code.

Example

var util = require('util');
var request = require('request');

function timedRequest(uri, callback) {
  var hrstart = process.hrtime();

  request.get(uri,
    function(err, res, body) {
      if (err) {
        return callback(err);
      }
      var hrend = process.hrtime(hrstart);
      util.log(util.format("%ds %dms %d GET %s", hrend[0], hrend[1] / 1000000, res.statusCode, uri));
      callback(null, res, body);
    });
}

... becomes ...

var util = require('util');
var request = require('request');
var failfast = require('fail-fast')

function timedRequest(uri, callback) {
  var hrstart = process.hrtime();

  request.get(uri,
    failfast(callback,
      function(res, body, cb) {
        var hrend = process.hrtime(hrstart);
        util.log(util.format("%ds %dms %d GET %s", hrend[0], hrend[1] / 1000000, res.statusCode, uri));
        cb(null, res, body);
      }));
}

Install

npm install fail-fast

Test

npm test

... or ...

mocha -R spec

To generate code-coverage of tests, run:

mocha --require blanket -R html-cov > coverage.html

You'll find the coverage report in the file coverage.html.

failfast

failfast is a function that takes the following arguments:

  • callback - (optional) the callback function that failfast will split error handling for,
  • continuation - (optional) a function where successful continuation proceeds,
  • thisArg - (optional) an object that will be used as the continuation's this context.

If you call provide no arguments to failfast, it will be benign (although it will still add overhead).

If callback is specified, it is invoked when failfast receives a truthy argument in position 0.

If continuation is specified, it is invoked when failfast receives a falsy argument in position 0. If callback was also specified, it is provided as the last argument to continuation. Any arguments beyond position 0 (the err argument) are forwarded in order to the specified continuation.

If thisArg is specified, it is bound to the this parameter when continuation is invoked.

A note about performance

Any time you extend the call stack you impact performance. An intermediary callback like failfast is no exception. Depending on your scenario the slight overhead may be acceptable - this is something you have to decide for yourself.

failfast is written in a manner that tries to minimize the overhead. Notably, it uses a technique I call switch-dispatch, which is similar to techniques taken by compilers and optimizers; it leverages what we already know, and produces a jump table to avoid copying the array of arguments. switch-dispatch would probably make you puke if you had to write it everywhere, but the performance is sweet.