Skip to content

Commit ca4ea17

Browse files
Thenkeiisaacs
authored andcommitted
fetch resolve to undefined rather than void
PR-URL: #303 Credit: @Thenkei Close: #303 Reviewed-by: @isaacs EDIT(@isaacs): edited to continue to allow `fetchMethod` to return `Promise<void>`. BREAKING CHANGE: public method type signature change
1 parent 01075cb commit ca4ea17

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class Stack {
167167
/**
168168
* Promise representing an in-progress {@link LRUCache#fetch} call
169169
*/
170-
export type BackgroundFetch<V> = Promise<V | undefined | void> & {
170+
export type BackgroundFetch<V> = Promise<V | undefined> & {
171171
__returned: BackgroundFetch<V> | undefined
172172
__abortController: AbortController
173173
__staleWhileFetching: V | undefined
@@ -500,7 +500,7 @@ export namespace LRUCache {
500500
key: K,
501501
staleValue: V | undefined,
502502
options: FetcherOptions<K, V, FC>
503-
) => Promise<V | void | undefined> | V | void | undefined
503+
) => Promise<V | undefined | void> | V | undefined | void
504504

505505
/**
506506
* Options which may be passed to the {@link LRUCache} constructor.
@@ -1868,9 +1868,9 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
18681868
}
18691869

18701870
const cb = (
1871-
v: V | void | undefined,
1871+
v: V | undefined,
18721872
updateCache = false
1873-
): V | undefined | void => {
1873+
): V | undefined => {
18741874
const { aborted } = ac.signal
18751875
const ignoreAbort = options.ignoreFetchAbort && v !== undefined
18761876
if (options.status) {
@@ -1943,12 +1943,12 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
19431943
}
19441944

19451945
const pcall = (
1946-
res: (v: V | void | undefined) => void,
1946+
res: (v: V | undefined) => void,
19471947
rej: (e: any) => void
19481948
) => {
19491949
const fmp = this.#fetchMethod?.(k, v, fetchOpts)
19501950
if (fmp && fmp instanceof Promise) {
1951-
fmp.then(v => res(v), rej)
1951+
fmp.then(v => res(v === undefined ? undefined : v), rej)
19521952
}
19531953
// ignored, we go until we finish, regardless.
19541954
// defer check until we are actually aborting,
@@ -1958,7 +1958,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
19581958
!options.ignoreFetchAbort ||
19591959
options.allowStaleOnFetchAbort
19601960
) {
1961-
res()
1961+
res(undefined)
19621962
// when it eventually resolves, update the cache.
19631963
if (options.allowStaleOnFetchAbort) {
19641964
res = v => cb(v, true)
@@ -1969,7 +1969,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
19691969

19701970
if (options.status) options.status.fetchDispatched = true
19711971
const p = new Promise(pcall).then(cb, eb)
1972-
const bf = Object.assign(p, {
1972+
const bf: BackgroundFetch<V> = Object.assign(p, {
19731973
__abortController: ac,
19741974
__staleWhileFetching: v,
19751975
__returned: undefined,
@@ -2020,7 +2020,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
20202020
: FC extends undefined | void
20212021
? LRUCache.FetchOptionsNoContext<K, V>
20222022
: LRUCache.FetchOptionsWithContext<K, V, FC>
2023-
): Promise<void | V>
2023+
): Promise<undefined | V>
20242024
// this overload not allowed if context is required
20252025
fetch(
20262026
k: unknown extends FC
@@ -2033,11 +2033,11 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
20332033
: FC extends undefined | void
20342034
? LRUCache.FetchOptionsNoContext<K, V>
20352035
: never
2036-
): Promise<void | V>
2036+
): Promise<undefined | V>
20372037
async fetch(
20382038
k: K,
20392039
fetchOptions: LRUCache.FetchOptions<K, V, FC> = {}
2040-
): Promise<void | V> {
2040+
): Promise<undefined | V> {
20412041
const {
20422042
// get options
20432043
allowStale = this.allowStale,

0 commit comments

Comments
 (0)