Skip to content

Commit f0488fb

Browse files
committed
fix: update return types to use Awaited for async functions
1 parent 8ab49d9 commit f0488fb

10 files changed

Lines changed: 61 additions & 51 deletions

File tree

.changeset/tiny-jobs-behave.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@tanstack/preact-pacer': patch
3+
'@tanstack/react-pacer': patch
4+
'@tanstack/pacer': patch
5+
---
6+
7+
fix: update return types to use Awaited for async functions

packages/pacer/src/async-debouncer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface AsyncDebouncerState<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
*/
@@ -111,7 +111,7 @@ export interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction> {
111111
* Optional callback to call when the debounced function is executed
112112
*/
113113
onSuccess?: (
114-
result: ReturnType<TFn>,
114+
result: Awaited<ReturnType<TFn>>,
115115
args: Parameters<TFn>,
116116
debouncer: AsyncDebouncer<TFn>,
117117
) => void
@@ -224,7 +224,7 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
224224
asyncRetryers = new Map<number, AsyncRetryer<TFn>>()
225225
#timeoutId: NodeJS.Timeout | null = null
226226
#resolvePreviousPromise:
227-
| ((value?: ReturnType<TFn> | undefined) => void)
227+
| ((value?: Awaited<ReturnType<TFn>> | undefined) => void)
228228
| null = null
229229

230230
constructor(
@@ -313,7 +313,7 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
313313
*/
314314
maybeExecute = async (
315315
...args: Parameters<TFn>
316-
): Promise<ReturnType<TFn> | undefined> => {
316+
): Promise<Awaited<ReturnType<TFn>> | undefined> => {
317317
if (!this.#getEnabled()) return undefined
318318
this.#cancelPendingExecution()
319319
this.#setState({
@@ -333,7 +333,7 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
333333
this.#setState({ isPending: true })
334334
}
335335

336-
return new Promise((resolve, reject) => {
336+
return new Promise<Awaited<ReturnType<TFn>> | undefined>((resolve, reject) => {
337337
this.#resolvePreviousPromise = resolve
338338
// this.#rejectPreviousPromise = reject
339339
this.#timeoutId = setTimeout(async () => {
@@ -356,7 +356,7 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
356356

357357
#execute = async (
358358
...args: Parameters<TFn>
359-
): Promise<ReturnType<TFn> | undefined> => {
359+
): Promise<Awaited<ReturnType<TFn>> | undefined> => {
360360
if (!this.#getEnabled()) return undefined
361361
const currentMaybeExecuteCount = this.store.state.maybeExecuteCount + 1
362362

@@ -372,7 +372,7 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
372372
lastResult: result,
373373
successCount: this.store.state.successCount + 1,
374374
})
375-
this.options.onSuccess?.(result as ReturnType<TFn>, args, this)
375+
this.options.onSuccess?.(result as Awaited<ReturnType<TFn>>, args, this)
376376
} catch (error) {
377377
this.#setState({
378378
errorCount: this.store.state.errorCount + 1,
@@ -397,7 +397,7 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
397397
/**
398398
* Processes the current pending execution immediately
399399
*/
400-
flush = async (): Promise<ReturnType<TFn> | undefined> => {
400+
flush = async (): Promise<Awaited<ReturnType<TFn>> | undefined> => {
401401
if (this.store.state.isPending && this.store.state.lastArgs) {
402402
const { lastArgs } = this.store.state
403403
this.#cancelPendingExecution()

packages/pacer/src/async-rate-limiter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface AsyncRateLimiterState<TFn extends AnyAsyncFunction> {
2525
/**
2626
* The result from the most recent successful function execution
2727
*/
28-
lastResult: ReturnType<TFn> | undefined
28+
lastResult: Awaited<ReturnType<TFn>> | undefined
2929
/**
3030
* Number of function executions that have been rejected due to rate limiting
3131
*/
@@ -118,7 +118,7 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
118118
* Optional function to call when the rate-limited function is executed
119119
*/
120120
onSuccess?: (
121-
result: ReturnType<TFn>,
121+
result: Awaited<ReturnType<TFn>>,
122122
args: Parameters<TFn>,
123123
rateLimiter: AsyncRateLimiter<TFn>,
124124
) => void
@@ -353,7 +353,7 @@ export class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
353353
*/
354354
maybeExecute = async (
355355
...args: Parameters<TFn>
356-
): Promise<ReturnType<TFn> | undefined> => {
356+
): Promise<Awaited<ReturnType<TFn>> | undefined> => {
357357
this.#setState({
358358
maybeExecuteCount: this.store.state.maybeExecuteCount + 1,
359359
})
@@ -376,7 +376,7 @@ export class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
376376

377377
#execute = async (
378378
...args: Parameters<TFn>
379-
): Promise<ReturnType<TFn> | undefined> => {
379+
): Promise<Awaited<ReturnType<TFn>> | undefined> => {
380380
if (!this.#getEnabled()) return
381381

382382
const currentMaybeExecute = this.store.state.maybeExecuteCount
@@ -400,7 +400,7 @@ export class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
400400
successCount: this.store.state.successCount + 1,
401401
lastResult: result,
402402
})
403-
this.options.onSuccess?.(result as ReturnType<TFn>, args, this)
403+
this.options.onSuccess?.(result as Awaited<ReturnType<TFn>>, args, this)
404404
} catch (error) {
405405
this.#setState({
406406
errorCount: this.store.state.errorCount + 1,

packages/pacer/src/async-throttler.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

packages/preact-pacer/src/async-debouncer/useAsyncDebouncedCallback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ import type { AnyAsyncFunction } from '@tanstack/pacer/types'
4444
export function useAsyncDebouncedCallback<TFn extends AnyAsyncFunction>(
4545
fn: TFn,
4646
options: AsyncDebouncerOptions<TFn>,
47-
): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {
47+
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>>> {
4848
const asyncDebouncedFn = useAsyncDebouncer(fn, options).maybeExecute
4949
return useCallback(
5050
(...args: Parameters<TFn>) =>
51-
asyncDebouncedFn(...args) as Promise<ReturnType<TFn>>,
51+
asyncDebouncedFn(...args) as Promise<Awaited<ReturnType<TFn>>>,
5252
[asyncDebouncedFn],
5353
)
5454
}

packages/preact-pacer/src/async-rate-limiter/useAsyncRateLimitedCallback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ import type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter
5959
export function useAsyncRateLimitedCallback<TFn extends AnyAsyncFunction>(
6060
fn: TFn,
6161
options: AsyncRateLimiterOptions<TFn>,
62-
): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {
62+
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>>> {
6363
const asyncRateLimitedFn = useAsyncRateLimiter(fn, options).maybeExecute
6464
return useCallback(
6565
(...args: Parameters<TFn>) =>
66-
asyncRateLimitedFn(...args) as Promise<ReturnType<TFn>>,
66+
asyncRateLimitedFn(...args) as Promise<Awaited<ReturnType<TFn>>>,
6767
[asyncRateLimitedFn],
6868
)
6969
}

packages/preact-pacer/src/async-throttler/useAsyncThrottledCallback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ import type { AnyAsyncFunction } from '@tanstack/pacer/types'
4242
export function useAsyncThrottledCallback<TFn extends AnyAsyncFunction>(
4343
fn: TFn,
4444
options: AsyncThrottlerOptions<TFn>,
45-
): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {
45+
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>>> {
4646
const asyncThrottledFn = useAsyncThrottler(fn, options).maybeExecute
4747
return useCallback(
4848
(...args: Parameters<TFn>) =>
49-
asyncThrottledFn(...args) as Promise<ReturnType<TFn>>,
49+
asyncThrottledFn(...args) as Promise<Awaited<ReturnType<TFn>>>,
5050
[asyncThrottledFn],
5151
)
5252
}

packages/react-pacer/src/async-debouncer/useAsyncDebouncedCallback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ import type { AnyAsyncFunction } from '@tanstack/pacer/types'
4444
export function useAsyncDebouncedCallback<TFn extends AnyAsyncFunction>(
4545
fn: TFn,
4646
options: AsyncDebouncerOptions<TFn>,
47-
): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {
47+
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>>> {
4848
const asyncDebouncedFn = useAsyncDebouncer(fn, options).maybeExecute
4949
return useCallback(
50-
(...args) => asyncDebouncedFn(...args) as Promise<ReturnType<TFn>>,
50+
(...args) => asyncDebouncedFn(...args) as Promise<Awaited<ReturnType<TFn>>>,
5151
[asyncDebouncedFn],
5252
)
5353
}

packages/react-pacer/src/async-rate-limiter/useAsyncRateLimitedCallback.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ import type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter
5959
export function useAsyncRateLimitedCallback<TFn extends AnyAsyncFunction>(
6060
fn: TFn,
6161
options: AsyncRateLimiterOptions<TFn>,
62-
): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {
62+
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>>> {
6363
const asyncRateLimitedFn = useAsyncRateLimiter(fn, options).maybeExecute
6464
return useCallback(
65-
(...args) => asyncRateLimitedFn(...args) as Promise<ReturnType<TFn>>,
65+
(...args) =>
66+
asyncRateLimitedFn(...args) as Promise<Awaited<ReturnType<TFn>>>,
6667
[asyncRateLimitedFn],
6768
)
6869
}

packages/react-pacer/src/async-throttler/useAsyncThrottledCallback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import type { AnyAsyncFunction } from '@tanstack/pacer/types'
4242
export function useAsyncThrottledCallback<TFn extends AnyAsyncFunction>(
4343
fn: TFn,
4444
options: AsyncThrottlerOptions<TFn>,
45-
): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {
45+
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>>> {
4646
const asyncThrottledFn = useAsyncThrottler(fn, options).maybeExecute
4747
return useCallback(
48-
(...args) => asyncThrottledFn(...args) as Promise<ReturnType<TFn>>,
48+
(...args) => asyncThrottledFn(...args) as Promise<Awaited<ReturnType<TFn>>>,
4949
[asyncThrottledFn],
5050
)
5151
}

0 commit comments

Comments
 (0)