es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
36 lines (35 loc) • 1.27 kB
JavaScript
/**
* Creates a memoized version of an asynchronous function.
* @param {AnyFunction} fn - The asynchronous function to memoize.
* @param {number} [expirationTime=60000] - Time in milliseconds before cache expires.
* @returns A memoized version of the input function.
* @example
* const memoizedFetch = memoizeAsync(async (url: string) => {
* const response = await fetch(url);
* return response.json();
* });
*
* // First call will fetch data
* const data1 = await memoizedFetch('https://api.example.com/data');
* // Second call with the same URL will return cached data
* const data2 = await memoizedFetch('https://api.example.com/data');
*/
export function memoizeAsync(fn, expirationTime = 60000) {
const cache = new Map();
return async function (...args) {
const key = JSON.stringify(args);
const now = Date.now();
if (cache.has(key)) {
const cachedEntry = cache.get(key);
if (now - cachedEntry.timestamp < expirationTime) {
return cachedEntry.value;
}
else {
cache.delete(key);
}
}
const result = await fn(...args);
cache.set(key, { value: result, timestamp: now });
return result;
};
}