Skip to content
This repository has been archived by the owner on Jan 31, 2021. It is now read-only.

Commit

Permalink
Better callback misuse detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Apr 4, 2015
1 parent 1028af5 commit 63df487
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion EventEmitterEx.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,16 @@
}

function callback (position, err/* arguments */) {
assert(! Array.isArray(result[position]),
'Callback called more than once by function at position ' + position + ' (0-based)');

if (err) {
firstError = firstError || err;
result[position] = [];
} else {
result[position] = slice(arguments, 2);
}
len--;
assert(len >= 0, 'Callback called more than once for each mapAsync() function!');
if (! len) {
if (firstError) {
eex.emit('error', firstError);
Expand Down
14 changes: 13 additions & 1 deletion test/EventEmitterEx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,19 @@
cb(null);
expect(function () {
cb(null);
}).to.throw(Error, 'Callback called more than once for each mapAsync() function!');
}).to.throw(Error, 'Callback called more than once by function at position 0 (0-based)');
done();
});
mapped.on('error', done);
emitter.emit('end');
});

it('should throw if callback called too many times (with misbehaving callback)', function (done) {
var mapped = emitter.mapAsync(function () {/* bad function, does not call cb() */}, function (cb) {
cb(null);
expect(function () {
cb(null);
}).to.throw(Error, 'Callback called more than once by function at position 1 (0-based)');
done();
});
mapped.on('error', done);
Expand Down

0 comments on commit 63df487

Please sign in to comment.