diff --git a/src/core/request.js b/src/core/request.js index 93f2a08a..ea9046d4 100644 --- a/src/core/request.js +++ b/src/core/request.js @@ -43,16 +43,19 @@ Request.prototype.request = function(servers, callback_) { const self = this; const callback = typeof servers === 'function' ? servers : callback_; + if (this.requested) { + throw new Error('Already requested'); + } + this.emit('before'); - - const wasRequested = this.requested; - this.requested = true; - this.callback(callback); - - if (wasRequested) { + // emit handler can set requested flag + if (this.requested) { return this; } + this.requested = true; + this.callback(callback); + this.on('error', function() {}); this.emit('request', this.remote); diff --git a/test/request-test.js b/test/request-test.js index c1e1ee6b..a606bba6 100644 --- a/test/request-test.js +++ b/test/request-test.js @@ -40,13 +40,12 @@ const SERVER_INFO = { }; describe('Request', function() { - it('Send request', function(done) { + it('Send request', function() { const remote = { request: function(req) { assert(req instanceof Request); assert.strictEqual(typeof req.message, 'object'); assert.strictEqual(req.message.command, 'server_info'); - done(); }, on: function() { }, @@ -60,7 +59,10 @@ describe('Request', function() { request.request(); // Should only request once - request.request(); + assert.throws(function() { + request.request(); + }, Error); + }); it('Send request -- filterRequest', function(done) {