@@ -44,7 +44,7 @@ import { parseSampleRate } from './utils/parseSampleRate';
4444import { prepareEvent } from './utils/prepareEvent' ;
4545import { reparentChildSpans , shouldIgnoreSpan } from './utils/should-ignore-span' ;
4646import { getActiveSpan , showSpanDropWarning , spanToTraceContext } from './utils/spanUtils' ;
47- import { rejectedSyncPromise , resolvedSyncPromise , SyncPromise } from './utils/syncpromise' ;
47+ import { rejectedSyncPromise } from './utils/syncpromise' ;
4848import { convertSpanJsonToTransactionEvent , convertTransactionEventToSpanJson } from './utils/transactionEvent' ;
4949
5050const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured." ;
@@ -316,16 +316,19 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
316316 * @returns A promise that will resolve with `true` if all events are sent before the timeout, or `false` if there are
317317 * still events in the queue when the timeout is reached.
318318 */
319- public flush ( timeout ?: number ) : PromiseLike < boolean > {
319+ // @ts -expect-error - PromiseLike is a subset of Promise
320+ public async flush ( timeout ?: number ) : PromiseLike < boolean > {
320321 const transport = this . _transport ;
321- if ( transport ) {
322- this . emit ( 'flush' ) ;
323- return this . _isClientDoneProcessing ( timeout ) . then ( clientFinished => {
324- return transport . flush ( timeout ) . then ( transportFlushed => clientFinished && transportFlushed ) ;
325- } ) ;
326- } else {
327- return resolvedSyncPromise ( true ) ;
322+ if ( ! transport ) {
323+ return Promise . resolve ( true ) ;
328324 }
325+
326+ this . emit ( 'flush' ) ;
327+
328+ const clientFinished = await this . _isClientDoneProcessing ( timeout ) ;
329+ const transportFlushed = await transport . flush ( timeout ) ;
330+
331+ return clientFinished && transportFlushed ;
329332 }
330333
331334 /**
@@ -336,12 +339,12 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
336339 * @returns {Promise<boolean> } A promise which resolves to `true` if the flush completes successfully before the timeout, or `false` if
337340 * it doesn't.
338341 */
339- public close ( timeout ?: number ) : PromiseLike < boolean > {
340- return this . flush ( timeout ) . then ( result = > {
341- this . getOptions ( ) . enabled = false ;
342- this . emit ( 'close' ) ;
343- return result ;
344- } ) ;
342+ // @ts -expect-error - PromiseLike is a subset of Promise
343+ public async close ( timeout ?: number ) : PromiseLike < boolean > {
344+ const result = await this . flush ( timeout ) ;
345+ this . getOptions ( ) . enabled = false ;
346+ this . emit ( 'close' ) ;
347+ return result ;
345348 }
346349
347350 /**
@@ -415,10 +418,9 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
415418 env = addItemToEnvelope ( env , createAttachmentEnvelopeItem ( attachment ) ) ;
416419 }
417420
418- const promise = this . sendEnvelope ( env ) ;
419- if ( promise ) {
420- promise . then ( sendResponse => this . emit ( 'afterSendEvent' , event , sendResponse ) , null ) ;
421- }
421+ // sendEnvelope should not throw
422+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
423+ this . sendEnvelope ( env ) . then ( sendResponse => this . emit ( 'afterSendEvent' , event , sendResponse ) ) ;
422424 }
423425
424426 /**
@@ -873,19 +875,21 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
873875 /**
874876 * Send an envelope to Sentry.
875877 */
876- public sendEnvelope ( envelope : Envelope ) : PromiseLike < TransportMakeRequestResponse > {
878+ // @ts -expect-error - PromiseLike is a subset of Promise
879+ public async sendEnvelope ( envelope : Envelope ) : PromiseLike < TransportMakeRequestResponse > {
877880 this . emit ( 'beforeEnvelope' , envelope ) ;
878881
879- if ( this . _isEnabled ( ) && this . _transport ) {
880- return this . _transport . send ( envelope ) . then ( null , reason => {
881- DEBUG_BUILD && debug . error ( 'Error while sending envelope:' , reason ) ;
882- return reason ;
883- } ) ;
882+ if ( ! this . _isEnabled ( ) || ! this . _transport ) {
883+ DEBUG_BUILD && debug . error ( 'Transport disabled' ) ;
884+ return { } ;
884885 }
885886
886- DEBUG_BUILD && debug . error ( 'Transport disabled' ) ;
887-
888- return resolvedSyncPromise ( { } ) ;
887+ try {
888+ return await this . _transport . send ( envelope ) ;
889+ } catch ( reason ) {
890+ DEBUG_BUILD && debug . error ( 'Error while sending envelope:' , reason ) ;
891+ return { } ;
892+ }
889893 }
890894
891895 /* eslint-enable @typescript-eslint/unified-signatures */
@@ -940,24 +944,20 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
940944 * @returns A promise which will resolve to `true` if processing is already done or finishes before the timeout, and
941945 * `false` otherwise
942946 */
943- protected _isClientDoneProcessing ( timeout ?: number ) : PromiseLike < boolean > {
944- return new SyncPromise ( resolve => {
945- let ticked : number = 0 ;
946- const tick : number = 1 ;
947+ protected async _isClientDoneProcessing ( timeout ?: number ) : Promise < boolean > {
948+ let ticked = 0 ;
947949
948- const interval = setInterval ( ( ) => {
949- if ( this . _numProcessing == 0 ) {
950- clearInterval ( interval ) ;
951- resolve ( true ) ;
952- } else {
953- ticked += tick ;
954- if ( timeout && ticked >= timeout ) {
955- clearInterval ( interval ) ;
956- resolve ( false ) ;
957- }
958- }
959- } , tick ) ;
960- } ) ;
950+ // if no timeout is provided, we wait "forever" until everything is processed
951+ while ( ! timeout || ticked < timeout ) {
952+ await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ;
953+
954+ if ( ! this . _numProcessing ) {
955+ return true ;
956+ }
957+ ticked ++ ;
958+ }
959+
960+ return false ;
961961 }
962962
963963 /** Determines whether this SDK is enabled and a transport is present. */
0 commit comments