Skip to content

API Reference

Constructor

typescript
new AsyncLoader<T, M = Record<string, any>>(
    loader: IAsyncLoader<T, M> | IAsyncLoader<T, M>[],
    options?: AsyncLoaderOptions<T, M>
)
  • loader: the underlying loader function (single or array), receiving a merged abort signal via args.abortSignal; may return Promise<T> or a synchronous value T. With multiple loaders, orchestrate via multiLoaderStrategy (see Multi-Loader).
  • options: constructor options, see Options.

Instance methods

MethodDescription
get(args?)Get the result; auto-triggers loading on first call / cache stale / last failure. args is forwarded to the internal signal (timeout is a wait timeout, semantically different from options.timeout)
load()Trigger a load (usually no need to call manually; get() triggers it automatically)
refresh(args?)Force a reload ignoring fresh cache; aborts any inflight load first, then reloads and returns the new result. args is forwarded to the internal signal
invalidate()Mark the data stale: clears the cache entry and resets a completed signal, so the next get() reloads. Does not trigger loading immediately. Unlike clear(), it also works without a cache (cache=0)
abort()Abort loading, penetrating to the underlying request; also terminates any pending retry wait
clear()Clear the current instance's cache entry
clearAll()Clear all cache entries in the shared storage
isPending()Whether loading is in progress (including retries); based on loading
isFulfilled()Whether the load succeeded (including defaultValue fallback)
isRejected()Whether the load failed (business error / timeout / abort)

Static methods

MethodDescription
AsyncLoader.resolve(result)Create a preset-fulfilled instance: get() returns result directly without loading (see Static Methods)
AsyncLoader.reject(error)Create a preset-rejected instance: get() throws error directly without loading (see Static Methods)
AsyncLoader.clearLoaderCache()Clear the multiplex inflight-instance cache; mainly for test isolation between cases sharing a hash

Instance properties

PropertyDescription
signalThe internal IAsyncSignal carrying the result; observe its state and result / error
loadingWhether a load (including retries) is in progress
loaderThe underlying loader function (single or array, constructor's first argument)
hashThe cache/multiplex key (explicit or auto-generated)
optionsThe merged constructor options

Error types

TriggerError type
Timeout final failure (retries exhausted or retry disabled)TimeoutError
Manual abort() / external abortSignalAbortError
Business errorThe original error is propagated as-is

Released under the MIT License.