4 ms·
Please correct me if I'm wrong. But you'd better avoid creating extra promises for performance reasons const promiseMap<key, Promise<any>> = new Map(); as
by blue_pants 4y ago
Please correct me if I'm wrong. But you'd better avoid creating extra promises for performance reasons
const promiseMap<key, Promise<any>> = new Map();
async function keyedDebounce<R>(key: string, fn: () => R) {
const existingPromise = promiseMap.get(key);
if (existingPromise) return existingPromise;
const promise = fn().then(result => {
promiseMap.delete(key);
return result;
});
promiseMap.set(key, promise);
return promise;
}