Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/imrandil/promise_javascript_reloaded_real_world_example

promise javascript leet code practice question into real world like implementation
https://github.com/imrandil/promise_javascript_reloaded_real_world_example

grinding javascript leetcode promise real-world-problem-solving

Last synced: 7 days ago
JSON representation

promise javascript leet code practice question into real world like implementation

Awesome Lists containing this project

README

        

# Time Limit Function (One of the code blocks)

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.

## Usage

```javascript
/**
* 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]);
};
};