Skip to content

Commit

Permalink
fix(asyncify): fix to avoid double callback
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru03 committed Jun 24, 2016
1 parent c52d8a3 commit 9a90db2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -9501,7 +9501,7 @@
if (result && typeof result === obj && typeof result.then === func) {
result.then(function(value) {
callback(null, value);
})['catch'](function(err) {
}, function(err) {
callback(err instanceof Error ? err : new Error(err));
});
} else {
Expand Down
26 changes: 25 additions & 1 deletion test/utils/test.asyncify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* global it */
/* global it, Promise */
'use strict';

var assert = require('assert');

var parallel = require('mocha.parallel');

var async = require('../../');
var delay = require('../config').delay;
var Q = require('q');

parallel('#asyncify', function() {
Expand Down Expand Up @@ -102,4 +103,27 @@ parallel('#asyncify', function() {
});
});

it('should avoid double callback', function(done) {

var count = 0;
var msg = 'error in callback';
var promisified = function(arg) {
return new Promise(function(resolve) {
resolve(arg + ' resolved');
});
};
async.asyncify(promisified)('arg', function(err, res) {
count++;
if (err) {
return done(err);
}
assert.strictEqual(res, 'arg resolved');
setTimeout(function() {
assert.strictEqual(count, 1);
done();
}, delay);
throw new Error(msg);
});
});

});

0 comments on commit 9a90db2

Please sign in to comment.