wrap-fn
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "wrap-fn", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "support sync, async, and generator functions", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
0.1.2 / 2015-01-16 | ||
================== | ||
* catch uncaught synchronous errors in async functions | ||
0.1.1 / 2014-09-24 | ||
================== | ||
* Add "scripts" field in `component.json` | ||
0.1.0 / 2014-09-17 | ||
@@ -3,0 +13,0 @@ ================== |
31
index.js
@@ -5,5 +5,4 @@ /** | ||
var slice = [].slice; | ||
var noop = function(){}; | ||
var co = require('co'); | ||
var noop = function(){}; | ||
@@ -27,6 +26,11 @@ /** | ||
function wrap(fn, done) { | ||
done = done || noop; | ||
done = once(done || noop); | ||
return function() { | ||
var args = slice.call(arguments); | ||
// prevents arguments leakage | ||
// see https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments | ||
var i = arguments.length; | ||
var args = new Array(i); | ||
while (i--) args[i] = arguments[i]; | ||
var ctx = this; | ||
@@ -41,3 +45,8 @@ | ||
if (fn.length > args.length) { | ||
return fn.apply(ctx, args.concat(done)); | ||
// NOTE: this only handles uncaught synchronous errors | ||
try { | ||
fn.apply(ctx, args.concat(done)); | ||
} catch (e) { | ||
done(e); | ||
} | ||
} | ||
@@ -108,1 +117,13 @@ | ||
} | ||
/** | ||
* Once | ||
*/ | ||
function once(fn) { | ||
return function() { | ||
var ret = fn.apply(this, arguments); | ||
fn = noop; | ||
return ret; | ||
}; | ||
} |
{ | ||
"name": "wrap-fn", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "support sync, async, and generator functions", | ||
@@ -5,0 +5,0 @@ "scripts": { |
76411
1558