Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback can be called twice in asyncify when using promises. #1197

Merged
merged 2 commits into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized, can you remove this after the test completes? process.off or only bind it with process.once

Please make a followup pr


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