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

outcome

v0.0.18

Published

DRY error handling

Downloads

421,388

Readme

Outcome.js is a simple flow-control library which wraps your .callback(err, result) functions.

Motiviation

  • Write less code for handling errors.
  • Easier to maintain.
  • Keep error handling code separate.

Basic Example

Here's the traditional method of handling errors:


var fs = require('fs');

function doSomething(path, callback) {

	fs.realpath(path, onRealPath);

	function onRealPath(err, path) {
		if(err) return callback(err);
		fs.lstat(path, onStat);
	}

	function onStat(err, stats) {
		if(err) return callback(err);
		callback(err, stats);
	}

}

doSomething('/path/to/something', function(err, result) {
	
	//inline with result handling - yuck
	if(err) {

		//do something with error
		return;
	}

	//do something with result
})

The outcome.js way:


var fs  = require('fs'),
outcome = require('outcome');

function doSomething(path, callback) {

	//wrap the callback around an error handler so any errors in *this* function
	//bubble back up to the callback - I'm lazy and I don't wanna write this stuff...
	var on = outcome.error(callback);

	//on success, call onRealPath. Any errors caught will be sent back
	//automatically
	fs.realpath(path, on.success(onRealPath));

	function onRealPath(path) {

		//ONLY call onStat if we've successfuly grabbed the file stats
		fs.lstat(path, on.success(onStat));
	}

	function onStat(stats) {

		//no errors, so send a response back
		callback(null, stats);
	}
}


var on = outcome.error(function(error) {
	//do something with error
}));

doSomething('/path/to/something', on.success(function(response) {
	//do something with result
}));

API

outcome(listeners)

  • listeners - Object of listeners you want to attach to outcome.

var onResult = outcome({
	
	//called when an error is caught
	error: function(error) {
		
	},

	//called when an error is NOT present
	success: function(result, thirdParam) {
		
	},

	//called back when an error, or result is present
	callback: function(err, result, thirdParam) {
		
	}
})

As shown in the example above, you can also wrap-around an existing callback:

var onResult = outcome.error(function(error) {
	
}).
success(function(result, thirdParam) {
	
}).
callback(function(error, result, thirdParam) {
	
});

By default, any unhandled errors are thrown. To get around this, you'll need to listen for an unhandledError:

outcome.on('unhandledError', function(error) {
	//report bugs here..., then throw again.
});


//fails
fs.stat('s'+__filename, outcome.success(function() {


});

.callback()

Called when on error/success. Same as function(err, data) { }

Here's a redundant example:


fs.stat(__filename, outcome.error(function(err) {
	//handle error
}).success(function(data) {
	//handle result
}.callback(function(err, result) {
	//called on fn complete regardless if there's an error, or success
}));

.success(fn)

Called on Success.

var onOutcome = outcome.success(function(data, anotherParam, andAnotherParam) {
	//handle success data
});

onOutcome(null, "success!", "more data!", "more results..");

.error(fn)

Called on error.


var onOutcome = outcome.error(function(err) {
	
});

onOutcome(new Error("something went wrong...")); 

.handle(fn)

Custom response handler


outcome.handle(function(response) {
	
	if(response.errors) this.error(response);
	if(response.data) this.success(response);
});

CoffeeScript Example


outcome = require "outcome"

doSomething(path, callback) ->
	
	on = outcome.error callback

	# first get the realpath
	fs.realpath path, on.success onRealPath

	# on real path, get stats
	onRealPath(path) -> fs.lstat path, on.success onStat

	# on stat, finish
	onStat(stats) -> callback null, stats


# call do something
doSomething '/path/to/something', outcome 

	success: (statis) ->
		# do something

	error: (error) ->
		# do something else

Note

Calling .error(), .success(), .callback() generates a new function which copies the previous listeners. Checkout fs-test in the examples folder.