Skip to content

Commit

Permalink
Fix: Wrap callback call in try/catch and rethrow async (fixes #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jan 15, 2018
1 parent ad873f2 commit e54c104
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions index.js
Expand Up @@ -11,6 +11,22 @@ var eosConfig = {
error: false,
};

function rethrowAsync(err) {
process.nextTick(rethrow);

function rethrow() {
throw err;
}
}

function tryCatch(fn, args) {
try {
return fn.apply(null, args);
} catch (err) {
rethrowAsync(err);
}
}

function asyncDone(fn, cb) {
cb = once(cb);

Expand All @@ -21,18 +37,18 @@ function asyncDone(fn, cb) {
function done() {
d.removeListener('error', onError);
d.exit();
return cb.apply(null, arguments);
return tryCatch(cb, arguments);
}

function onSuccess(result) {
tick(done, null, result);
done(null, result);
}

function onError(error) {
if (!error) {
error = new Error('Promise rejected without Error');
}
tick(done, error);
done(error);
}

function asyncRunner() {
Expand Down
1 change: 1 addition & 0 deletions test/promises.js
Expand Up @@ -48,6 +48,7 @@ describe('promises', function() {
var d = domain.create();
d.once('error', function(err) {
expect(err).toExist();
expect(err.message).toContain('Boom');
done();
});
d.run(function() {
Expand Down

0 comments on commit e54c104

Please sign in to comment.