Time Out an Async Function
Wrap an async function so it gives up after a deadline — the building block behind every request timeout.
JavaScript · ES6 — Pratik Rai ·
A key/value cache where every entry expires on its own timer — and setting a key again restarts its clock.
Build an ExpiringCache class with three methods:
set(key, value, duration) stores the pair for duration ms and returns whether an unexpired entry for that key already existed.get(key) returns the value if it is still alive, otherwise -1.count() returns how many entries are currently alive.Input:
JSfile.javascript1const cache = new ExpiringCache(); 2cache.set(3, 30, 400); // false 3cache.set(8, 80, 100); // false 4// 250ms later 5[cache.get(3), cache.get(8), cache.count()];
Output:
[30, -1, 1]
Each entry runs on its own timer. Key 8 expired at 100ms, while key 3 stays alive until 400ms, so only one entry is still counted.
The tempting design stores an expiry timestamp and checks it on every read. That works, but then count() has to filter, get has to compare, and expired entries linger in memory until something happens to touch them.
Store the timeout id beside the value and let the timer delete the entry itself. Now the map only ever contains live entries, so get is a lookup and count is map.size.
JSfile.javascript1class ExpiringCache { 2 constructor() { this.entries = new Map(); } 3 set(key, value, duration) { 4 const existing = this.entries.get(key); 5 if (existing) clearTimeout(existing.timer); 6 const timer = setTimeout(() => this.entries.delete(key), duration); 7 this.entries.set(key, { value, timer }); 8 return Boolean(existing); 9 } 10 get(key) { 11 const entry = this.entries.get(key); 12 return entry ? entry.value : -1; 13 } 14 count() { return this.entries.size; } 15}
The subtlety is re-setting an existing key. Its old timer is still scheduled and still pointing at that key, so unless you clear it the earlier timeout fires and deletes an entry that was just refreshed. Clear first, then schedule.
set returns whether it overwrote a live entry, which falls out of checking the map before writing.
Goal: Every entry expires independently, and re-setting a key restarts its timer.
Continue learning with these related challenges
Wrap an async function so it gives up after a deadline — the building block behind every request timeout.
JavaScript · ES6 — Pratik Rai ·
A fixed-size cache that evicts whatever was used least recently — and does both reads and writes in constant time.
JavaScript · ES6 — Pratik Rai ·
Run a list of async tasks with a hard limit on how many are in flight at once — the concurrency control Promise.all does not give you.
JavaScript · ES6 — Pratik Rai ·
Wrap an async function so it gives up after a deadline — the building block behind every request timeout.
JavaScript · ES6
Pratik Rai ·
A fixed-size cache that evicts whatever was used least recently — and does both reads and writes in constant time.
JavaScript · ES6
Pratik Rai ·
Run a list of async tasks with a hard limit on how many are in flight at once — the concurrency control Promise.all does not give you.
JavaScript · ES6
Pratik Rai ·