async-deco
Advanced tools
Comparing version 5.0.3 to 5.0.4
{ | ||
"name": "async-deco", | ||
"version": "5.0.3", | ||
"version": "5.0.4", | ||
"description": "A collection of decorators for adding features to asynchronous functions (callback or promise based).", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -332,5 +332,6 @@ async-deco | ||
--------------------- | ||
This special decorator adds a couple of important checks to your callback based asynchronous functions. | ||
It captures any unhandled exception that has been throwed, and it uses it as "err" argument of the callback. | ||
If the callback is fired twice, the second time it will fire a "Callback fired twice" exception. | ||
This special decorator tries to take care of some nasty common cases when you work with "callback based" asynchronous functions. | ||
* if a function calls the callback more than once, the second time it will throw an exception instead | ||
* if a function throws an exception, it will instead call the callback with the error | ||
* if the callback itself throws an exception, it propagates the exception to the calling function | ||
```js | ||
@@ -355,3 +356,3 @@ var sanitizeAsyncFunction = require('async-deco/utils/sanitizeAsyncFunction'); | ||
func(function (err, out) { | ||
// the second time err will contain a "Callback fired twice" exception. | ||
// this will throw an exception instead of being called the second time | ||
}); | ||
@@ -358,0 +359,0 @@ ``` |
@@ -29,4 +29,5 @@ var defaultLogger = require('../utils/default-logger'); | ||
if (!err) { | ||
logger('cache-set', {args: args, res: res}); | ||
cache.push(args, res); | ||
if (cache.push(args, res)) { | ||
logger('cache-set', {args: args, res: res}); | ||
} | ||
} | ||
@@ -33,0 +34,0 @@ cb(err, res); |
@@ -47,4 +47,5 @@ var defaultLogger = require('../utils/default-logger'); | ||
if (!noPush) { | ||
logger('fallback-cache-set', {args: args, res: res}); | ||
cache.push(args, res); | ||
if (cache.push(args, res)) { | ||
logger('fallback-cache-set', {args: args, res: res}); | ||
} | ||
} | ||
@@ -51,0 +52,0 @@ |
@@ -28,3 +28,3 @@ var assert = require('chai').assert; | ||
it('must catch double called callback', function (done) { | ||
it('must catch double called callback', function () { | ||
var firstTime = true; | ||
@@ -37,15 +37,43 @@ | ||
func(1, 2, function (err, out) { | ||
if (firstTime) { | ||
assert.throws(function () { | ||
func(1, 2, function (err, out) { | ||
assert.isTrue(firstTime); | ||
assert.equal(out, 3); | ||
firstTime = false; | ||
} | ||
else { | ||
assert.instanceOf(err, Error); | ||
assert.equal(err.message, 'Callback fired twice'); | ||
done(); | ||
} | ||
}); | ||
}, 'Callback fired twice'); | ||
}); | ||
it('must catch double called callback (2)', function () { | ||
var firstTime = true; | ||
var func = sanitizeAsyncFunction(function (a, b, cb) { | ||
cb(null, a + b); | ||
throw new Error('generic error'); | ||
}); | ||
assert.throws(function () { | ||
func(1, 2, function (err, out) { | ||
assert.isTrue(firstTime); | ||
assert.equal(out, 3); | ||
firstTime = false; | ||
}); | ||
}, 'generic error'); | ||
}); | ||
it('must catch and throw errors on the callback', function () { | ||
var firstTime = true; | ||
var func = sanitizeAsyncFunction(function (a, b, cb) { | ||
cb(null, a + b); | ||
}); | ||
assert.throws(function () { | ||
func(1, 2, function (err, out) { | ||
assert.isTrue(firstTime); | ||
firstTime = false; | ||
throw(new Error('callback broken')); | ||
}); | ||
}, 'callback broken'); | ||
}); | ||
}); |
function safe (func) { | ||
function safe(func) { | ||
return function () { | ||
@@ -10,7 +10,7 @@ var context = this; | ||
if (alreadyFired) { | ||
cb(new Error('Callback fired twice')); | ||
throw new Error('Callback fired twice'); | ||
} | ||
else { | ||
alreadyFired = true; | ||
cb(err, res); | ||
alreadyFired = true; | ||
} | ||
@@ -23,2 +23,5 @@ }; | ||
catch (e) { | ||
if (alreadyFired) { | ||
throw e; | ||
} | ||
cb(e); | ||
@@ -25,0 +28,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99295
2680
514