This function is designed to limit the execution time of another function. It wraps the provided function with a time limit, ensuring that it does not exceed a specified duration.
/**
* Creates a time-limited version of a function.
* @param {Function} fn - The function to be time-limited.
* @param {number} t - The time limit in milliseconds.
* @returns {Function} - The time-limited function.
*/
var timeLimit = function(fn, t) {
return async function(...args) {
const originalFnPromise = fn(...args);
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject('Time Limit Exceeded');
}, t);
});
return Promise.race([originalFnPromise, timeoutPromise]);
};
};