Skip to content

Commit

Permalink
[asyncify] Make errors in callbacks throw globally
Browse files Browse the repository at this point in the history
If asyncify wraps a function returning promises, the callback will be executed as part of the promise’s `.then()` method.
This means that any error thrown from that method will be silenced, and lead to a “unhandled promise rejection” warning in modern engines.

Usually, we want these errors to be visible, and even crash the process.
  • Loading branch information
davidaurelio committed Apr 18, 2017
1 parent 996abc2 commit b987870
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions 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
Expand Down Expand Up @@ -68,9 +69,9 @@ export default function asyncify(func) {
// if result is Promise object
if (isObject(result) && typeof result.then === 'function') {
result.then(function(value) {
callback(null, value);
setImmediate(callback, null, value);
}, function(err) {
callback(err.message ? err : new Error(err));
setImmediate(callback, err.message ? err : new Error(err));
});
} else {
callback(null, result);
Expand Down

0 comments on commit b987870

Please sign in to comment.