Skip to main content

PromiseCache Class

A specialized cache for async work, allowing you to safely cache the promised result of some async work without fear of running it multiple times or losing track of errors.

This API is provided for existing users, but is not recommended for new users.

To use, import via @fluidframework/core-utils/legacy.

For more information about our API support guarantees, see here.

Signature

export declare class PromiseCache<TKey, TResult>

Type Parameters

ParameterDescription
TKey
TResult

Constructors

ConstructorAlertsDescription
(constructor)({ expiry, removeOnError, })Beta

Create the PromiseCache with the given options, with the following defaults:

expiry: indefinite, removeOnError: true for all errors

Methods

MethodAlertsReturn TypeDescription
add(key, asyncFn)BetabooleanTry to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.
addOrGet(key, asyncFn)BetaPromise<TResult>Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.
addValue(key, value)BetabooleanTry to add the given value, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.
addValueOrGet(key, value)BetaPromise<TResult>Try to add the given value, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.
get(key)BetaPromise<TResult> | undefinedGet the Promise for the given key, or undefined if it's not found. Extend expiry if applicable.
has(key)BetabooleanCheck if there's anything cached at the given key
remove(key)BetabooleanRemove the Promise for the given key, returning true if it was found and removed

Constructor Details

(constructor)

Create the PromiseCache with the given options, with the following defaults:

expiry: indefinite, removeOnError: true for all errors

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

constructor({ expiry, removeOnError, }?: PromiseCacheOptions);

Parameters

ParameterModifiersTypeDescription
{ expiry, removeOnError, }optionalPromiseCacheOptions

Method Details

add

Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

add(key: TKey, asyncFn: () => Promise<TResult>): boolean;

Parameters

ParameterTypeDescription
keyTKeykey name where to store the async work
asyncFn() => Promise<TResult>the async work to do and store, if not already in progress under the given key

Returns

Return type: boolean

addOrGet

Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult>;

Parameters

ParameterTypeDescription
keyTKeykey name where to store the async work
asyncFn() => Promise<TResult>the async work to do and store, if not already in progress under the given key

Returns

Return type: Promise<TResult>

addValue

Try to add the given value, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

addValue(key: TKey, value: TResult): boolean;

Parameters

ParameterTypeDescription
keyTKeykey name where to store the value
valueTResultvalue to store

Returns

Return type: boolean

addValueOrGet

Try to add the given value, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

addValueOrGet(key: TKey, value: TResult): Promise<TResult>;

Parameters

ParameterTypeDescription
keyTKeykey name where to store the async work
valueTResultvalue to store

Returns

Return type: Promise<TResult>

get

Get the Promise for the given key, or undefined if it's not found. Extend expiry if applicable.

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

get(key: TKey): Promise<TResult> | undefined;

Parameters

ParameterTypeDescription
keyTKey

Returns

Return type: Promise<TResult> | undefined

has

Check if there's anything cached at the given key

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

has(key: TKey): boolean;

Parameters

ParameterTypeDescription
keyTKey

Returns

Return type: boolean

remove

Remove the Promise for the given key, returning true if it was found and removed

This API is provided for existing users, but is not recommended for new users.

For more information about our API support guarantees, see here.

Signature

remove(key: TKey): boolean;

Parameters

ParameterTypeDescription
keyTKey

Returns

Return type: boolean