@@ -47,6 +47,7 @@ import {promisifySome} from './util';
4747import { StatusError } from './message-stream' ;
4848import { DebugMessage } from './debug' ;
4949import { EventEmitter } from 'stream' ;
50+ import { promisify } from 'util' ;
5051
5152export { AckError , AckResponse , AckResponses } from './subscriber' ;
5253
@@ -588,23 +589,34 @@ export class Subscription extends EventEmitter {
588589 const gaxOpts = typeof optsOrCallback === 'object' ? optsOrCallback : { } ;
589590 callback = typeof optsOrCallback === 'function' ? optsOrCallback : callback ;
590591
592+ const prom = this . _deleteAsync ( gaxOpts ) ;
593+ if ( callback ) {
594+ prom . then ( r => callback ( null , r ) ) . catch ( e => callback ( e ) ) ;
595+ } else {
596+ return prom ;
597+ }
598+ }
599+
600+ /*!
601+ * Async innards of delete() so we can wait for subscription.close() if needed.
602+ */
603+ async _deleteAsync ( gaxOpts : CallOptions ) : Promise < EmptyResponse > {
591604 const reqOpts = {
592605 subscription : this . name ,
593606 } ;
594607
595608 if ( this . isOpen ) {
596- this . _subscriber . close ( ) ;
609+ await this . _subscriber . close ( ) ;
597610 }
598611
599- this . request < google . protobuf . Empty > (
600- {
601- client : 'SubscriberClient' ,
602- method : 'deleteSubscription' ,
603- reqOpts,
604- gaxOpts,
605- } ,
606- callback ! ,
607- ) ;
612+ await promisify ( this . request < google . protobuf . Empty > ) ( {
613+ client : 'SubscriberClient' ,
614+ method : 'deleteSubscription' ,
615+ reqOpts,
616+ gaxOpts,
617+ } ) ;
618+
619+ return [ { } ] ;
608620 }
609621
610622 /**
@@ -1164,7 +1176,7 @@ export class Subscription extends EventEmitter {
11641176
11651177 this . on ( 'removeListener' , ( ) => {
11661178 if ( this . isOpen && this . listenerCount ( 'message' ) === 0 ) {
1167- this . _subscriber . close ( ) ;
1179+ void this . _subscriber . close ( ) ;
11681180 }
11691181 } ) ;
11701182 }
0 commit comments