@@ -161,16 +161,21 @@ class Pool extends EventEmitter {
161
161
throw new Error ( 'unexpected condition' )
162
162
}
163
163
164
- _remove ( client ) {
164
+ _remove ( client , callback ) {
165
165
const removed = removeWhere ( this . _idle , ( item ) => item . client === client )
166
166
167
167
if ( removed !== undefined ) {
168
168
clearTimeout ( removed . timeoutId )
169
169
}
170
170
171
171
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
+ } )
174
179
}
175
180
176
181
connect ( cb ) {
@@ -351,17 +356,15 @@ class Pool extends EventEmitter {
351
356
if ( client . _poolUseCount >= this . options . maxUses ) {
352
357
this . log ( 'remove expended client' )
353
358
}
354
- this . _remove ( client )
355
- this . _pulseQueue ( )
356
- return
359
+
360
+ return this . _remove ( client , this . _pulseQueue . bind ( this ) )
357
361
}
358
362
359
363
const isExpired = this . _expired . has ( client )
360
364
if ( isExpired ) {
361
365
this . log ( 'remove expired client' )
362
366
this . _expired . delete ( client )
363
- this . _remove ( client )
364
- this . _pulseQueue ( )
367
+ this . _remove ( client , this . _pulseQueue . bind ( this ) )
365
368
return
366
369
}
367
370
@@ -370,7 +373,7 @@ class Pool extends EventEmitter {
370
373
if ( this . options . idleTimeoutMillis && this . _isAboveMin ( ) ) {
371
374
tid = setTimeout ( ( ) => {
372
375
this . log ( 'remove idle client' )
373
- this . _remove ( client )
376
+ this . _remove ( client , this . _pulseQueue . bind ( this ) )
374
377
} , this . options . idleTimeoutMillis )
375
378
376
379
if ( this . options . allowExitOnIdle ) {
0 commit comments