[30 Days of JS] Interval Cancellation / Promise Time Limit
2025. 5. 18. 10:41ㆍ개인활동/코테
반응형
/**
* @param {Function} fn
* @param {Array} args
* @param {number} t
* @return {Function}
*/
var cancellable = function(fn, args, t) {
fn(...args);
const intervalId = setInterval(() => {
fn(...args);
}, t);
return () => clearInterval(intervalId);
};
/**
* const result = [];
*
* const fn = (x) => x * 2;
* const args = [4], t = 35, cancelTimeMs = 190;
*
* const start = performance.now();
*
* const log = (...argsArr) => {
* const diff = Math.floor(performance.now() - start);
* result.push({"time": diff, "returned": fn(...argsArr)});
* }
*
* const cancel = cancellable(log, args, t);
*
* setTimeout(cancel, cancelTimeMs);
*
* setTimeout(() => {
* console.log(result); // [
* // {"time":0,"returned":8},
* // {"time":35,"returned":8},
* // {"time":70,"returned":8},
* // {"time":105,"returned":8},
* // {"time":140,"returned":8},
* // {"time":175,"returned":8}
* // ]
* }, cancelTimeMs + t + 15)
*/
- fn 바로 실행 후
- Interval 함수를 통해 지정된 시간까지 Interval 동작 수행 후 Clear
/**
* @param {Function} fn
* @param {number} t
* @return {Function}
*/
var timeLimit = function(fn, t) {
return async function(...args) {
// Create a timeout promise that rejects after t milliseconds
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject("Time Limit Exceeded");
}, t);
});
// Race the original function and the timeout
return Promise.race([
fn(...args), // Original async function
timeoutPromise // Timeout rejection
]);
}
};
/**
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
*/
- reject를 사용해야 한다
- promise 만들어둔 뒤
- 입력받은 함수와 timeoutPromise를 하나의 Promise로 만들어주는 Promise.race를 통해 반환
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race
반응형
'개인활동 > 코테' 카테고리의 다른 글
[30 Days of JS] Sleep / Timeout Cancellation (0) | 2025.05.15 |
---|---|
[30 Days of JS] Memoize / Add two promises (1) | 2025.05.14 |
[30 Days of JS] Return length of arguments passed / (0) | 2025.05.11 |
[30 Days of JS] Array reduce transformation / Function composition (0) | 2025.05.10 |
[30 Days of JS] Apply Transform Over Each Element in Array / Filter Elements from Array (0) | 2025.05.09 |