Skip to content

Commit 99ba060

Browse files
committed
Ensure the promise and callback versions of Client#connect always have the same behaviour
1 parent 70a2b56 commit 99ba060

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lib/client.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ var Client = function (config) {
5353

5454
util.inherits(Client, EventEmitter)
5555

56-
Client.prototype.connect = function (callback) {
56+
Client.prototype._connect = function (callback) {
5757
var self = this
5858
var con = this.connection
5959
if (this._connecting || this._connected) {
6060
const err = new Error('Client has already been connected. You cannot reuse a client.')
61-
if (callback) {
61+
process.nextTick(() => {
6262
callback(err)
63-
return undefined
64-
}
65-
return Promise.reject(err)
63+
})
64+
return
6665
}
6766
this._connecting = true
6867

@@ -220,16 +219,23 @@ Client.prototype.connect = function (callback) {
220219
con.on('notice', function (msg) {
221220
self.emit('notice', msg)
222221
})
222+
}
223+
224+
Client.prototype.connect = function (callback) {
225+
if (callback) {
226+
this._connect(callback)
227+
return
228+
}
223229

224-
if (!callback) {
225-
return new global.Promise((resolve, reject) => {
226-
this.once('error', reject)
227-
this.once('connect', () => {
228-
this.removeListener('error', reject)
230+
return new Promise((resolve, reject) => {
231+
this._connect((error) => {
232+
if (error) {
233+
reject(error)
234+
} else {
229235
resolve()
230-
})
236+
}
231237
})
232-
}
238+
})
233239
}
234240

235241
Client.prototype._attachListeners = function (con) {

0 commit comments

Comments
 (0)