Skip to content

promise javascript leet code practice question into real world like implementation

Notifications You must be signed in to change notification settings

IMRANDIL/Promise_Javascript_Reloaded_real_world_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

About

promise javascript leet code practice question into real world like implementation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published