Promise Time Limit
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 a TimeLimitedCache 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 TimeLimitedCache(); 2cache.set(1, 42, 100); // false 3cache.get(1); // 42 4// 150ms later 5cache.get(1);
Output:
-1
The entry expired 50ms ago, so get reports a miss.
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 ·
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 ·
Convert a Node-style (error, value) callback API into one that returns a promise.
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 ·
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 ·
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6
Pratik Rai ·