From 99ba0605616eaee73c43228f45df6ec47577ab39 Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Fri, 4 May 2018 16:46:37 -0700 Subject: [PATCH] Ensure the promise and callback versions of Client#connect always have the same behaviour --- lib/client.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/client.js b/lib/client.js index 9653f7dcc..140f505b2 100644 --- a/lib/client.js +++ b/lib/client.js @@ -53,16 +53,15 @@ var Client = function (config) { util.inherits(Client, EventEmitter) -Client.prototype.connect = function (callback) { +Client.prototype._connect = function (callback) { var self = this var con = this.connection if (this._connecting || this._connected) { const err = new Error('Client has already been connected. You cannot reuse a client.') - if (callback) { + process.nextTick(() => { callback(err) - return undefined - } - return Promise.reject(err) + }) + return } this._connecting = true @@ -220,16 +219,23 @@ Client.prototype.connect = function (callback) { con.on('notice', function (msg) { self.emit('notice', msg) }) +} + +Client.prototype.connect = function (callback) { + if (callback) { + this._connect(callback) + return + } - if (!callback) { - return new global.Promise((resolve, reject) => { - this.once('error', reject) - this.once('connect', () => { - this.removeListener('error', reject) + return new Promise((resolve, reject) => { + this._connect((error) => { + if (error) { + reject(error) + } else { resolve() - }) + } }) - } + }) } Client.prototype._attachListeners = function (con) {