summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Early <alexander.early@gmail.com>2017-04-22 12:27:23 -0700
committerGitHub <noreply@github.com>2017-04-22 12:27:23 -0700
commitdd7cf796310d2a9ad8fa4ce1bf8ec976497bf8cd (patch)
tree642741ba98df4490188237b3f2f54bb9907d0aa8
parenteb95a0ab3f9a2b8e202cc8480b5af7fb9ec1942e (diff)
parentd45fac1af512a6890d9cf15148d9049ee27dffbd (diff)
downloadasync-dd7cf796310d2a9ad8fa4ce1bf8ec976497bf8cd.tar.gz
Merge pull request #1408 from davidaurelio/master
[asyncify] Make errors in callbacks throw globally
-rw-r--r--lib/asyncify.js17
-rw-r--r--mocha_test/asyncify.js38
2 files changed, 52 insertions, 3 deletions
diff --git a/lib/asyncify.js b/lib/asyncify.js
index 9292070..d5d0bee 100644
--- a/lib/asyncify.js
+++ b/lib/asyncify.js
@@ -1,5 +1,6 @@
import isObject from 'lodash/isObject';
import initialParams from './internal/initialParams';
+import setImmediate from './internal/setImmediate';
/**
* Take a sync function and make it async, passing its return value to a
@@ -68,12 +69,24 @@ export default function asyncify(func) {
// if result is Promise object
if (isObject(result) && typeof result.then === 'function') {
result.then(function(value) {
- callback(null, value);
+ invokeCallback(callback, null, value);
}, function(err) {
- callback(err.message ? err : new Error(err));
+ invokeCallback(callback, err.message ? err : new Error(err));
});
} else {
callback(null, result);
}
});
}
+
+function invokeCallback(callback, error, value) {
+ try {
+ callback(error, value);
+ } catch (e) {
+ setImmediate(rethrow, e);
+ }
+}
+
+function rethrow(error) {
+ throw error;
+}
diff --git a/mocha_test/asyncify.js b/mocha_test/asyncify.js
index a98826c..112b8ad 100644
--- a/mocha_test/asyncify.js
+++ b/mocha_test/asyncify.js
@@ -92,7 +92,9 @@ describe('asyncify', function(){
});
});
- it('callback error', function(done) {
+ it('callback error @nodeonly', function(done) {
+ expectUncaughtException();
+
var promisified = function(argument) {
return new Promise(function (resolve) {
resolve(argument + " resolved");
@@ -105,11 +107,30 @@ describe('asyncify', function(){
throw new Error("error in callback");
}
});
+
setTimeout(function () {
expect(call_count).to.equal(1);
done();
}, 15);
});
+
+ it('dont catch errors in the callback @nodeonly', function(done) {
+ expectUncaughtException(checkErr);
+ var callbackError = new Error('thrown from callback');
+
+ function checkErr(err) {
+ expect(err).to.equal(callbackError);
+ done();
+ }
+
+ function callback() {
+ throw callbackError;
+ }
+
+ async.asyncify(function () {
+ return Promise.reject(new Error('rejection'));
+ })(callback);
+ });
}
describe('native-promise-only', function() {
@@ -134,5 +155,20 @@ describe('asyncify', function(){
var Promise = require('rsvp').Promise;
promisifiedTests.call(this, Promise);
});
+
+ function expectUncaughtException(onError) {
+ // do a weird dance to catch the async thrown error before mocha
+ var listeners = process.listeners('uncaughtException');
+ process.removeAllListeners('uncaughtException');
+ process.once('uncaughtException', function onErr(err) {
+ listeners.forEach(function(listener) {
+ process.on('uncaughtException', listener);
+ });
+ // can't throw errors in a uncaughtException handler, defer
+ if (onError) {
+ setTimeout(onError, 0, err);
+ }
+ });
+ }
});
});