Skip to content

Commit e9466dd

Browse files
asadbek2021hjr3
authored andcommitted
Pass callback to client.end
1 parent 6be857e commit e9466dd

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

packages/pg-pool/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,21 @@ class Pool extends EventEmitter {
161161
throw new Error('unexpected condition')
162162
}
163163

164-
_remove(client) {
164+
_remove(client, callback) {
165165
const removed = removeWhere(this._idle, (item) => item.client === client)
166166

167167
if (removed !== undefined) {
168168
clearTimeout(removed.timeoutId)
169169
}
170170

171171
this._clients = this._clients.filter((c) => c !== client)
172-
client.end()
173-
this.emit('remove', client)
172+
client.end(() => {
173+
this.emit('remove', client)
174+
175+
if (typeof callback === "function") {
176+
callback()
177+
}
178+
})
174179
}
175180

176181
connect(cb) {
@@ -351,17 +356,15 @@ class Pool extends EventEmitter {
351356
if (client._poolUseCount >= this.options.maxUses) {
352357
this.log('remove expended client')
353358
}
354-
this._remove(client)
355-
this._pulseQueue()
356-
return
359+
360+
return this._remove(client, this._pulseQueue.bind(this))
357361
}
358362

359363
const isExpired = this._expired.has(client)
360364
if (isExpired) {
361365
this.log('remove expired client')
362366
this._expired.delete(client)
363-
this._remove(client)
364-
this._pulseQueue()
367+
this._remove(client, this._pulseQueue.bind(this))
365368
return
366369
}
367370

@@ -370,7 +373,7 @@ class Pool extends EventEmitter {
370373
if (this.options.idleTimeoutMillis && this._isAboveMin()) {
371374
tid = setTimeout(() => {
372375
this.log('remove idle client')
373-
this._remove(client)
376+
this._remove(client, this._pulseQueue.bind(this))
374377
}, this.options.idleTimeoutMillis)
375378

376379
if (this.options.allowExitOnIdle) {

0 commit comments

Comments
 (0)