Skip to content

Commit 3469495

Browse files
committed
added
1 parent 1cc8286 commit 3469495

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

CacheWithTimeLimnit.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key.
2+
3+
// The class has three public methods:
4+
5+
// set(key, value, duration): accepts an integer key, an integer value, and a duration in milliseconds. Once the duration has elapsed, the key should be inaccessible. The method should return true if the same un-expired key already exists and false otherwise. Both the value and duration should be overwritten if the key already exists.
6+
7+
// get(key): if an un-expired key exists, it should return the associated value. Otherwise it should return -1.
8+
9+
// count(): returns the count of un-expired keys.

PromiseTimeLImit.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Given an asynchronous function fn and a time t in milliseconds, return a new time limited version of the input function. fn takes arguments provided to the time limited function.
2+
3+
// The time limited function should follow these rules:
4+
5+
// If the fn completes within the time limit of t milliseconds, the time limited function should resolve with the result.
6+
// If the execution of the fn exceeds the time limit, the time limited function should reject with the string "Time Limit Exceeded".
7+
8+
var timeLimit = function (fn, t) {
9+
return async function (...args) {
10+
const onSuccess = fn(...args)
11+
const result = new Promise((res, rej) => {
12+
setTimeout(() => {
13+
rej("Time Limit Exceeded")
14+
}, t)
15+
})
16+
return Promise.race([onSuccess,result])
17+
}
18+
}

0 commit comments

Comments
 (0)