Skip to content

Commit

Permalink
Merge pull request #1197 from raydog/master
Browse files Browse the repository at this point in the history
Callback can be called twice in asyncify when using promises.
  • Loading branch information
megawac committed Jun 23, 2016
2 parents 1706048 + 3ef90da commit e739104
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/asyncify.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function asyncify(func) {
if (isObject(result) && typeof result.then === 'function') {
result.then(function(value) {
callback(null, value);
})['catch'](function(err) {
}, function(err) {
callback(err.message ? err : new Error(err));
});
} else {
Expand Down
26 changes: 26 additions & 0 deletions mocha_test/asyncify.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ describe('asyncify', function(){
'rsvp'
];

// Both Bluebird and native promises emit these events. We handle it because Bluebird
// will report these rejections to stderr if we don't, which is a great feature for
// normal cases, but not here, since we expect unhandled rejections:
process.on('unhandledRejection', function () {
// Ignore.
});

names.forEach(function(name) {
describe(name, function() {

Expand Down Expand Up @@ -111,6 +118,25 @@ describe('asyncify', function(){
done();
});
});

it('callback error', function(done) {
var promisified = function(argument) {
return new Promise(function (resolve) {
resolve(argument + " resolved");
});
};
var call_count = 0;
async.asyncify(promisified)("argument", function () {
call_count++;
if (call_count === 1) {
throw new Error("error in callback");
}
});
setTimeout(function () {
expect(call_count).to.equal(1);
done();
}, 15);
});
});
});
});
Expand Down

0 comments on commit e739104

Please sign in to comment.