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

Parameter Description
TKey
TResult

Constructors

Constructor Alerts Description
(constructor)({ expiry, removeOnError, }) Beta

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

expiry: indefinite, removeOnError: true for all errors

Methods

Method Alerts Return Type Description
add(key, asyncFn) Beta boolean 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.
addOrGet(key, asyncFn) Beta Promise<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) Beta boolean 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.
addValueOrGet(key, value) Beta Promise<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) Beta Promise<TResult> | undefined Get the Promise for the given key, or undefined if it's not found. Extend expiry if applicable.
has(key) Beta boolean Check if there's anything cached at the given key
remove(key) Beta boolean Remove 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

Parameter Modifiers Type Description
{ expiry, removeOnError, } optional PromiseCacheOptions

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

Parameter Type Description
key TKey key 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

Parameter Type Description
key TKey key 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

Parameter Type Description
key TKey key name where to store the value
value TResult value 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

Parameter Type Description
key TKey key name where to store the async work
value TResult value 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

Parameter Type Description
key TKey

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

Parameter Type Description
key TKey

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

Parameter Type Description
key TKey

Returns

Return type: boolean