summaryrefslogtreecommitdiff
path: root/lib/asyncify.js
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 /lib/asyncify.js
parenteb95a0ab3f9a2b8e202cc8480b5af7fb9ec1942e (diff)
parentd45fac1af512a6890d9cf15148d9049ee27dffbd (diff)
downloadasync-dd7cf796310d2a9ad8fa4ce1bf8ec976497bf8cd.tar.gz
Merge pull request #1408 from davidaurelio/master
[asyncify] Make errors in callbacks throw globally
Diffstat (limited to 'lib/asyncify.js')
-rw-r--r--lib/asyncify.js17
1 files changed, 15 insertions, 2 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;
+}