{"_id":"outcome","_rev":"107237","name":"outcome","description":"DRY error handling","dist-tags":{"latest":"0.0.18"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"time":{"modified":"2021-06-03T10:39:07.000Z","created":"2011-12-23T20:19:16.683Z","0.0.18":"2013-03-29T22:58:23.741Z","0.0.17":"2013-03-17T22:28:47.180Z","0.0.16":"2013-03-17T20:43:18.225Z","0.0.14":"2013-03-11T05:39:58.812Z","0.0.12":"2013-02-13T02:01:49.175Z","0.0.10":"2012-12-30T22:17:13.697Z","0.0.7":"2012-12-04T01:09:26.336Z","0.0.6":"2012-08-20T12:54:11.401Z","0.0.5":"2012-02-17T21:12:23.831Z","0.0.3":"2011-12-31T01:36:47.582Z","0.0.1":"2011-12-23T20:19:16.683Z"},"users":{"jjdanois":true},"author":{"name":"Craig Condon"},"repository":{},"versions":{"0.0.18":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{},"version":"0.0.18","engines":{},"dependencies":{},"devDependencies":{"expect.js":"0.2.x","mocha":"1.8.x"},"scripts":{"test":"mocha --reporter min"},"main":"./lib/index.js","readmeFilename":"README.md","_id":"outcome@0.0.18","dist":{"shasum":"7a7720232a4bc5631c27ca80705b6dde7536e63b","size":11109,"noattachment":false,"key":"/outcome/-/outcome-0.0.18.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.18.tgz"},"_from":".","_npmVersion":"1.2.11","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1364597903741,"_cnpm_publish_time":1364597903741,"_hasShrinkwrap":false},"0.0.17":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{},"version":"0.0.17","engines":{},"dependencies":{},"devDependencies":{"expect.js":"0.2.x","mocha":"1.8.x"},"scripts":{"test":"mocha --reporter min"},"main":"./lib/index.js","readmeFilename":"README.md","_id":"outcome@0.0.17","dist":{"shasum":"6e1e99a0e6a03472abf39912ee73c561ce05fd98","size":11121,"noattachment":false,"key":"/outcome/-/outcome-0.0.17.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.17.tgz"},"_from":".","_npmVersion":"1.2.11","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1363559327180,"_cnpm_publish_time":1363559327180,"_hasShrinkwrap":false},"0.0.16":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{},"version":"0.0.16","engines":{},"dependencies":{},"devDependencies":{"expect.js":"0.2.x","mocha":"1.8.x"},"scripts":{"test":"mocha --reporter min"},"main":"./lib/index.js","readmeFilename":"README.md","_id":"outcome@0.0.16","dist":{"shasum":"1974d929c7fff07bb39a7c3e1777984ed1834b9e","size":11146,"noattachment":false,"key":"/outcome/-/outcome-0.0.16.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.16.tgz"},"_from":".","_npmVersion":"1.2.11","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1363552998225,"_cnpm_publish_time":1363552998225,"_hasShrinkwrap":false},"0.0.14":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{},"version":"0.0.14","engines":{},"dependencies":{},"devDependencies":{"expect.js":"0.2.x","mocha":"1.8.x"},"scripts":{"test":"mocha --reporter min"},"main":"./lib/index.js","readmeFilename":"README.md","_id":"outcome@0.0.14","dist":{"shasum":"1162532dee191d0149101491be4ebc3ae0eabea2","size":11046,"noattachment":false,"key":"/outcome/-/outcome-0.0.14.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.14.tgz"},"_from":".","_npmVersion":"1.2.11","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1362980398812,"_cnpm_publish_time":1362980398812,"_hasShrinkwrap":false},"0.0.12":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{},"version":"0.0.12","engines":{},"dependencies":{},"devDependencies":{},"main":"./lib/index.js","readmeFilename":"README.md","_id":"outcome@0.0.12","dist":{"shasum":"081d4d4ef6ce965744c6a6d874a81bbd998f9bf2","size":10399,"noattachment":false,"key":"/outcome/-/outcome-0.0.12.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.12.tgz"},"_npmVersion":"1.1.66","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1360720909175,"_cnpm_publish_time":1360720909175,"_hasShrinkwrap":false},"0.0.10":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{},"version":"0.0.10","engines":{},"dependencies":{},"devDependencies":{},"main":"./lib/index.js","readmeFilename":"README.md","_id":"outcome@0.0.10","dist":{"shasum":"4318e0217babb519d298cf08a50830ba1b89dc12","size":10274,"noattachment":false,"key":"/outcome/-/outcome-0.0.10.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.10.tgz"},"_npmVersion":"1.1.65","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1356905833697,"_cnpm_publish_time":1356905833697,"_hasShrinkwrap":false},"0.0.7":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{},"version":"0.0.7","engines":{},"dependencies":{},"devDependencies":{},"main":"./lib/index.js","readmeFilename":"README.md","_id":"outcome@0.0.7","dist":{"shasum":"585fd2ec6227c0e467e45e5a18293cb9a5921463","size":12567,"noattachment":false,"key":"/outcome/-/outcome-0.0.7.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.7.tgz"},"_npmVersion":"1.1.65","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1354583366336,"_cnpm_publish_time":1354583366336,"_hasShrinkwrap":false},"0.0.6":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{"url":""},"version":"0.0.6","engines":{"node":"*"},"dependencies":{},"devDependencies":{},"main":"./lib/index.js","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"_id":"outcome@0.0.6","_engineSupported":true,"_npmVersion":"1.0.106","_nodeVersion":"v0.6.12","_defaultsLoaded":true,"dist":{"shasum":"5a11b96afdbfe8453a312ec32a70f00cb8fc10c1","size":20480,"noattachment":false,"key":"/outcome/-/outcome-0.0.6.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.6.tgz"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1345467251401,"_cnpm_publish_time":1345467251401,"_hasShrinkwrap":false},"0.0.5":{"name":"outcome","description":"DRY error handling","author":{"name":"Craig Condon"},"repository":{"url":""},"version":"0.0.5","engines":{"node":"*"},"dependencies":{},"devDependencies":{},"main":"./lib/index.js","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"_id":"outcome@0.0.5","_engineSupported":true,"_npmVersion":"1.0.106","_nodeVersion":"v0.4.12","_defaultsLoaded":true,"dist":{"shasum":"f34eb2d2cbe3b115313eeccac371e9f04eace12f","size":20480,"noattachment":false,"key":"/outcome/-/outcome-0.0.5.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.5.tgz"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1329513143831,"_cnpm_publish_time":1329513143831,"_hasShrinkwrap":false},"0.0.3":{"name":"outcome","description":"Better Result Handling","version":"0.0.3","repository":{"url":""},"engines":{"node":"*"},"dependencies":{"underscore":"*"},"devDependencies":{},"main":"./lib/index.js","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"_id":"outcome@0.0.3","_engineSupported":true,"_npmVersion":"1.0.106","_nodeVersion":"v0.4.12","_defaultsLoaded":true,"dist":{"shasum":"945fc38fb08bb569aaf09d78f008b4a386934761","size":10240,"noattachment":false,"key":"/outcome/-/outcome-0.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.3.tgz"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1325295407582,"_cnpm_publish_time":1325295407582,"_hasShrinkwrap":false},"0.0.1":{"name":"outcome","description":"Better Result Handling","version":"0.0.1","repository":{"url":""},"engines":{"node":"*"},"dependencies":{"tq":"<=0.1.0","structr":"*"},"devDependencies":{},"main":"./lib/index.js","_npmUser":{"name":"architectd","email":"craig.j.condon@gmail.com"},"_id":"outcome@0.0.1","_engineSupported":true,"_npmVersion":"1.0.106","_nodeVersion":"v0.4.12","_defaultsLoaded":true,"dist":{"shasum":"20ebd0fcb152bce07410b341d41fdac5065f19a9","size":10240,"noattachment":false,"key":"/outcome/-/outcome-0.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/outcome/download/outcome-0.0.1.tgz"},"maintainers":[{"name":"architectd","email":"craig.j.condon@gmail.com"},{"name":"crcn","email":"craig.j.condon@gmail.com"}],"directories":{},"publish_time":1324671556683,"_cnpm_publish_time":1324671556683,"_hasShrinkwrap":false}},"readme":"\n\nOutcome.js is a simple flow-control library which wraps your `.callback(err, result)` functions. \n\n### Motiviation\n\n- Write less code for handling errors.\n- Easier to maintain.\n- Keep error handling code separate. \n\n\n\n### Basic Example\n\nHere's the traditional method of handling errors:\n\n```javascript\n\nvar fs = require('fs');\n\nfunction doSomething(path, callback) {\n\n\tfs.realpath(path, onRealPath);\n\n\tfunction onRealPath(err, path) {\n\t\tif(err) return callback(err);\n\t\tfs.lstat(path, onStat);\n\t}\n\n\tfunction onStat(err, stats) {\n\t\tif(err) return callback(err);\n\t\tcallback(err, stats);\n\t}\n\n}\n\ndoSomething('/path/to/something', function(err, result) {\n\t\n\t//inline with result handling - yuck\n\tif(err) {\n\n\t\t//do something with error\n\t\treturn;\n\t}\n\n\t//do something with result\n})\n```\n\nThe outcome.js way:\n\n```javascript\n\nvar fs  = require('fs'),\noutcome = require('outcome');\n\nfunction doSomething(path, callback) {\n\n\t//wrap the callback around an error handler so any errors in *this* function\n\t//bubble back up to the callback - I'm lazy and I don't wanna write this stuff...\n\tvar on = outcome.error(callback);\n\n\t//on success, call onRealPath. Any errors caught will be sent back\n\t//automatically\n\tfs.realpath(path, on.success(onRealPath));\n\n\tfunction onRealPath(path) {\n\n\t\t//ONLY call onStat if we've successfuly grabbed the file stats\n\t\tfs.lstat(path, on.success(onStat));\n\t}\n\n\tfunction onStat(stats) {\n\n\t\t//no errors, so send a response back\n\t\tcallback(null, stats);\n\t}\n}\n\n\nvar on = outcome.error(function(error) {\n\t//do something with error\n}));\n\ndoSomething('/path/to/something', on.success(function(response) {\n\t//do something with result\n}));\n\n```\n\n## API\n\n### outcome(listeners)\n\n- `listeners` - Object of listeners you want to attach to outcome.\n\n```javascript\n\nvar onResult = outcome({\n\t\n\t//called when an error is caught\n\terror: function(error) {\n\t\t\n\t},\n\n\t//called when an error is NOT present\n\tsuccess: function(result, thirdParam) {\n\t\t\n\t},\n\n\t//called back when an error, or result is present\n\tcallback: function(err, result, thirdParam) {\n\t\t\n\t}\n})\n\n```\n\nAs shown in the example above, you can also wrap-around an existing callback:\n\n```javascript\nvar onResult = outcome.error(function(error) {\n\t\n}).\nsuccess(function(result, thirdParam) {\n\t\n}).\ncallback(function(error, result, thirdParam) {\n\t\n});\n```\n\n\nBy default, any unhandled errors are thrown. To get around this, you'll need to listen for an `unhandledError`:\n\n```javascript\noutcome.on('unhandledError', function(error) {\n\t//report bugs here..., then throw again.\n});\n\n\n//fails\nfs.stat('s'+__filename, outcome.success(function() {\n\n\n});\n```\n\n\n\n### .callback()\n\nCalled when on error/success. `Same as function(err, data) { }`\n\nHere's a redundant example:\n\n```javascript\n\nfs.stat(__filename, outcome.error(function(err) {\n\t//handle error\n}).success(function(data) {\n\t//handle result\n}.callback(function(err, result) {\n\t//called on fn complete regardless if there's an error, or success\n}));\n\n```\n\n### .success(fn)\n\nCalled on Success.\n\n```javascript\nvar onOutcome = outcome.success(function(data, anotherParam, andAnotherParam) {\n\t//handle success data\n});\n\nonOutcome(null, \"success!\", \"more data!\", \"more results..\");\n```\n\n### .error(fn)\n\nCalled on error.\n\n```javascript\n\nvar onOutcome = outcome.error(function(err) {\n\t\n});\n\nonOutcome(new Error(\"something went wrong...\")); \n```\n\n### .handle(fn)\n\nCustom response handler\n\n```javascript\n\noutcome.handle(function(response) {\n\t\n\tif(response.errors) this.error(response);\n\tif(response.data) this.success(response);\n});\n\n```\n\n\n## CoffeeScript Example\n\n```coffeescript\n\noutcome = require \"outcome\"\n\ndoSomething(path, callback) ->\n\t\n\ton = outcome.error callback\n\n\t# first get the realpath\n\tfs.realpath path, on.success onRealPath\n\n\t# on real path, get stats\n\tonRealPath(path) -> fs.lstat path, on.success onStat\n\n\t# on stat, finish\n\tonStat(stats) -> callback null, stats\n\n\n# call do something\ndoSomething '/path/to/something', outcome \n\n\tsuccess: (statis) ->\n\t\t# do something\n\n\terror: (error) ->\n\t\t# do something else\n\n\n```\n\n\n### Note\n\nCalling `.error()`, `.success()`, `.callback()` generates a new function which copies the previous listeners. \nCheckout [fs-test](outcome.js/blob/master/examples/fs-test.js) in the [examples](outcome.js/blog/master/examples) folder.\n","_attachments":{},"readmeFilename":"README.md"}