From ca4ea17eb262e49e7ea5ead992f0837b353ef86b Mon Sep 17 00:00:00 2001 From: Thenkei Date: Wed, 31 May 2023 11:58:07 +0200 Subject: [PATCH] fetch resolve to undefined rather than void PR-URL: https://github.com/isaacs/node-lru-cache/pull/303 Credit: @Thenkei Close: #303 Reviewed-by: @isaacs EDIT(@isaacs): edited to continue to allow `fetchMethod` to return `Promise`. BREAKING CHANGE: public method type signature change --- src/index.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index 029c654..eeb4ed8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -167,7 +167,7 @@ class Stack { /** * Promise representing an in-progress {@link LRUCache#fetch} call */ -export type BackgroundFetch = Promise & { +export type BackgroundFetch = Promise & { __returned: BackgroundFetch | undefined __abortController: AbortController __staleWhileFetching: V | undefined @@ -500,7 +500,7 @@ export namespace LRUCache { key: K, staleValue: V | undefined, options: FetcherOptions - ) => Promise | V | void | undefined + ) => Promise | V | undefined | void /** * Options which may be passed to the {@link LRUCache} constructor. @@ -1868,9 +1868,9 @@ export class LRUCache { } const cb = ( - v: V | void | undefined, + v: V | undefined, updateCache = false - ): V | undefined | void => { + ): V | undefined => { const { aborted } = ac.signal const ignoreAbort = options.ignoreFetchAbort && v !== undefined if (options.status) { @@ -1943,12 +1943,12 @@ export class LRUCache { } const pcall = ( - res: (v: V | void | undefined) => void, + res: (v: V | undefined) => void, rej: (e: any) => void ) => { const fmp = this.#fetchMethod?.(k, v, fetchOpts) if (fmp && fmp instanceof Promise) { - fmp.then(v => res(v), rej) + fmp.then(v => res(v === undefined ? undefined : v), rej) } // ignored, we go until we finish, regardless. // defer check until we are actually aborting, @@ -1958,7 +1958,7 @@ export class LRUCache { !options.ignoreFetchAbort || options.allowStaleOnFetchAbort ) { - res() + res(undefined) // when it eventually resolves, update the cache. if (options.allowStaleOnFetchAbort) { res = v => cb(v, true) @@ -1969,7 +1969,7 @@ export class LRUCache { if (options.status) options.status.fetchDispatched = true const p = new Promise(pcall).then(cb, eb) - const bf = Object.assign(p, { + const bf: BackgroundFetch = Object.assign(p, { __abortController: ac, __staleWhileFetching: v, __returned: undefined, @@ -2020,7 +2020,7 @@ export class LRUCache { : FC extends undefined | void ? LRUCache.FetchOptionsNoContext : LRUCache.FetchOptionsWithContext - ): Promise + ): Promise // this overload not allowed if context is required fetch( k: unknown extends FC @@ -2033,11 +2033,11 @@ export class LRUCache { : FC extends undefined | void ? LRUCache.FetchOptionsNoContext : never - ): Promise + ): Promise async fetch( k: K, fetchOptions: LRUCache.FetchOptions = {} - ): Promise { + ): Promise { const { // get options allowStale = this.allowStale,