@@ -156,16 +156,21 @@ class Pool extends EventEmitter {
156156 throw new Error ( 'unexpected condition' )
157157 }
158158
159- _remove ( client ) {
159+ _remove ( client , callback ) {
160160 const removed = removeWhere ( this . _idle , ( item ) => item . client === client )
161161
162162 if ( removed !== undefined ) {
163163 clearTimeout ( removed . timeoutId )
164164 }
165165
166166 this . _clients = this . _clients . filter ( ( c ) => c !== client )
167- client . end ( )
168- this . emit ( 'remove' , client )
167+ client . end ( ( ) => {
168+ this . emit ( 'remove' , client )
169+
170+ if ( typeof callback === "function" ) {
171+ callback ( )
172+ }
173+ } )
169174 }
170175
171176 connect ( cb ) {
@@ -342,17 +347,15 @@ class Pool extends EventEmitter {
342347 if ( client . _poolUseCount >= this . options . maxUses ) {
343348 this . log ( 'remove expended client' )
344349 }
345- this . _remove ( client )
346- this . _pulseQueue ( )
347- return
350+
351+ return this . _remove ( client , this . _pulseQueue . bind ( this ) )
348352 }
349353
350354 const isExpired = this . _expired . has ( client )
351355 if ( isExpired ) {
352356 this . log ( 'remove expired client' )
353357 this . _expired . delete ( client )
354- this . _remove ( client )
355- this . _pulseQueue ( )
358+ this . _remove ( client , this . _pulseQueue . bind ( this ) )
356359 return
357360 }
358361
@@ -361,7 +364,7 @@ class Pool extends EventEmitter {
361364 if ( this . options . idleTimeoutMillis ) {
362365 tid = setTimeout ( ( ) => {
363366 this . log ( 'remove idle client' )
364- this . _remove ( client )
367+ this . _remove ( client , this . _pulseQueue . bind ( this ) )
365368 } , this . options . idleTimeoutMillis )
366369
367370 if ( this . options . allowExitOnIdle ) {
0 commit comments