Skip to content

Commit

Permalink
Ensure the promise and callback versions of Client#connect always hav…
Browse files Browse the repository at this point in the history
…e the same behaviour
  • Loading branch information
charmander committed May 4, 2018
1 parent 70a2b56 commit 99ba060
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/client.js
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 99ba060

Please sign in to comment.