diff --git a/lib/client.js b/lib/client.js index 852b3087..6db60566 100644 --- a/lib/client.js +++ b/lib/client.js @@ -28,7 +28,7 @@ sys.inherits(Client, EventEmitter); var p = Client.prototype; -p.connect = function() { +p.connect = function(callback) { var self = this; var con = this.connection; if(this.host && this.host.indexOf('/') === 0) { @@ -84,11 +84,17 @@ p.connect = function() { } }); - self.emit('connect'); + if (!callback) { + self.emit('connect'); + } else { + callback(null,self); + //remove callback for proper error handling after the connect event + callback = null; + } con.on('notification', function(msg) { self.emit('notification', msg); - }) + }); }); @@ -103,7 +109,11 @@ p.connect = function() { con.on('error', function(error) { if(!self.activeQuery) { - self.emit('error', error); + if(!callback) { + self.emit('error', error); + } else { + callback(error); + } } else { //need to sync after error during a prepared statement if(self.activeQuery.isPreparedStatement) { @@ -116,7 +126,7 @@ p.connect = function() { con.on('notice', function(msg) { self.emit('notice', msg); - }) + }); };