mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
lib/client: added optional callback function to client.prototype.connect(); issue #52
This commit is contained in:
parent
aded1af4e5
commit
5f7e85162a
@ -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);
|
||||
})
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user