Skip to content

Commit 5f7e851

Browse files
booobrianc
authored andcommitted
lib/client: added optional callback function to client.prototype.connect(); issue brianc#52
1 parent aded1af commit 5f7e851

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/client.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ sys.inherits(Client, EventEmitter);
2828

2929
var p = Client.prototype;
3030

31-
p.connect = function() {
31+
p.connect = function(callback) {
3232
var self = this;
3333
var con = this.connection;
3434
if(this.host && this.host.indexOf('/') === 0) {
@@ -84,11 +84,17 @@ p.connect = function() {
8484
}
8585
});
8686

87-
self.emit('connect');
87+
if (!callback) {
88+
self.emit('connect');
89+
} else {
90+
callback(null,self);
91+
//remove callback for proper error handling after the connect event
92+
callback = null;
93+
}
8894

8995
con.on('notification', function(msg) {
9096
self.emit('notification', msg);
91-
})
97+
});
9298

9399
});
94100

@@ -103,7 +109,11 @@ p.connect = function() {
103109

104110
con.on('error', function(error) {
105111
if(!self.activeQuery) {
106-
self.emit('error', error);
112+
if(!callback) {
113+
self.emit('error', error);
114+
} else {
115+
callback(error);
116+
}
107117
} else {
108118
//need to sync after error during a prepared statement
109119
if(self.activeQuery.isPreparedStatement) {
@@ -116,7 +126,7 @@ p.connect = function() {
116126

117127
con.on('notice', function(msg) {
118128
self.emit('notice', msg);
119-
})
129+
});
120130

121131
};
122132

0 commit comments

Comments
 (0)