@@ -29,7 +29,7 @@ export interface AsyncThrottlerState<TFn extends AnyAsyncFunction> {
2929 /**
3030 * The result from the most recent successful function execution
3131 */
32- lastResult : ReturnType < TFn > | undefined
32+ lastResult : Awaited < ReturnType < TFn > > | undefined
3333 /**
3434 * Number of times maybeExecute has been called (for reduction calculations)
3535 */
@@ -236,7 +236,7 @@ export class AsyncThrottler<TFn extends AnyAsyncFunction> {
236236 asyncRetryers = new Map < number , AsyncRetryer < TFn > > ( )
237237 #timeoutId: NodeJS . Timeout | null = null
238238 #resolvePreviousPromise:
239- | ( ( value ?: ReturnType < TFn > | undefined ) => void )
239+ | ( ( value ?: Awaited < ReturnType < TFn > > | undefined ) => void )
240240 | null = null
241241
242242 constructor (
@@ -333,7 +333,7 @@ export class AsyncThrottler<TFn extends AnyAsyncFunction> {
333333 */
334334 maybeExecute = async (
335335 ...args : Parameters < TFn >
336- ) : Promise < ReturnType < TFn > | undefined > => {
336+ ) : Promise < Awaited < ReturnType < TFn > > | undefined > => {
337337 if ( ! this . #getEnabled( ) ) return undefined
338338
339339 this . #resolvePreviousPromiseInternal( )
@@ -376,34 +376,36 @@ export class AsyncThrottler<TFn extends AnyAsyncFunction> {
376376 } )
377377
378378 // Set up new trailing execution
379- return new Promise ( ( resolve , reject ) => {
380- this . #resolvePreviousPromise = resolve
381-
382- const newTimeSinceLastExecution = this . store . state . lastExecutionTime
383- ? now - this . store . state . lastExecutionTime
384- : 0
385- const timeoutDuration = Math . max ( 0 , wait - newTimeSinceLastExecution )
386-
387- this . #timeoutId = setTimeout ( async ( ) => {
388- this . #clearTimeout( )
389- if ( this . store . state . lastArgs !== undefined ) {
390- try {
391- await this . #execute( ...this . store . state . lastArgs ) // Trailing EXECUTE!
392- } catch ( error ) {
393- reject ( error )
379+ return new Promise < Awaited < ReturnType < TFn > > | undefined > (
380+ ( resolve , reject ) => {
381+ this . #resolvePreviousPromise = resolve
382+
383+ const newTimeSinceLastExecution = this . store . state . lastExecutionTime
384+ ? now - this . store . state . lastExecutionTime
385+ : 0
386+ const timeoutDuration = Math . max ( 0 , wait - newTimeSinceLastExecution )
387+
388+ this . #timeoutId = setTimeout ( async ( ) => {
389+ this . #clearTimeout( )
390+ if ( this . store . state . lastArgs !== undefined ) {
391+ try {
392+ await this . #execute( ...this . store . state . lastArgs ) // Trailing EXECUTE!
393+ } catch ( error ) {
394+ reject ( error )
395+ }
394396 }
395- }
396- this . #resolvePreviousPromise = null
397- resolve ( this . store . state . lastResult )
398- } , timeoutDuration )
399- } )
397+ this . #resolvePreviousPromise = null
398+ resolve ( this . store . state . lastResult )
399+ } , timeoutDuration )
400+ } ,
401+ )
400402 }
401403 return this . store . state . lastResult
402404 }
403405
404406 #execute = async (
405407 ...args : Parameters < TFn >
406- ) : Promise < ReturnType < TFn > | undefined > => {
408+ ) : Promise < Awaited < ReturnType < TFn > > | undefined > => {
407409 if ( ! this . #getEnabled( ) ) return undefined
408410
409411 const currentMaybeExecute = this . store . state . maybeExecuteCount
@@ -420,7 +422,7 @@ export class AsyncThrottler<TFn extends AnyAsyncFunction> {
420422 lastResult : result ,
421423 successCount : this . store . state . successCount + 1 ,
422424 } )
423- this . options . onSuccess ?.( result as ReturnType < TFn > , args , this )
425+ this . options . onSuccess ?.( result as Awaited < ReturnType < TFn > > , args , this )
424426 } catch ( error ) {
425427 this . #setState( {
426428 errorCount : this . store . state . errorCount + 1 ,
@@ -455,7 +457,7 @@ export class AsyncThrottler<TFn extends AnyAsyncFunction> {
455457 /**
456458 * Processes the current pending execution immediately
457459 */
458- flush = async ( ) : Promise < ReturnType < TFn > | undefined > => {
460+ flush = async ( ) : Promise < Awaited < ReturnType < TFn > > | undefined > => {
459461 if ( this . store . state . isPending && this . store . state . lastArgs ) {
460462 // Store the pending promise resolver before clearing timeout
461463 const resolvePromise = this . #resolvePreviousPromise
0 commit comments