Interface AsyncResourceOptions<T>

interface AsyncResourceOptions<T> {
    autoReload?: boolean;
    autoReloadAfter?: number;
    fetcher: (
        ctx: AsyncResourceContext<T>,
    ) => Promise<{ data: T; expiresIn: number }>;
    onError?: (err: unknown, ctx: AsyncResourceContext<T>) => void;
    swr?: boolean;
    swrValidator?: (ctx: AsyncResourceContext<T>) => boolean;
}

Type Parameters

  • T

Properties

autoReload?: boolean

if true, the resource will be automatically reloaded after autoReloadAfter milliseconds once the resource is expires

(note that it will need to be fetched manually at least once)

autoReloadAfter?: number

time in milliseconds after which the resource will be reloaded after expiring if autoReload is true. can be negative to reload before expiring

0 (i.e. as soon as it expires)
fetcher: (
    ctx: AsyncResourceContext<T>,
) => Promise<{ data: T; expiresIn: number }>

function that will be called to fetch the resource

Type declaration

    • (ctx: AsyncResourceContext<T>): Promise<{ data: T; expiresIn: number }>
    • Parameters

      Returns Promise<{ data: T; expiresIn: number }>

      Promise that resolves to the resource and the number of milliseconds before it expires

onError?: (err: unknown, ctx: AsyncResourceContext<T>) => void

function that will be called if fetcher throws an error

Type declaration

console.error(err)

swr?: boolean

if true, the resource will keep returning expired data after it expires while the resource is being reloaded

swrValidator?: (ctx: AsyncResourceContext<T>) => boolean

if swr is true, this function will be called to validate if the cached data is still valid