Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

a decorator function that slows down the execution of another function javascript

a decorator function that slows down the execution of an arbitrary function by 5 seconds.

function someFunction(a, b) {
        console.log(a + b);
    }
function slower(func, seconds) {
// decorator code
}
let slowedSomeFunction = slower(someFunction, 5);
slowedSomeFunction()
// will output to the console "You will get you result in 5 seconds"
//...in 5 seconds will display the result of 'someFunction*'

attempts were unsuccessful, there are errors in the code

function someFunction(x) {
    alert(x);
}

function slower(func, seconds) {
    return function() {
        setTimeout(() => func.apply(this, arguments), seconds);
    }
}

let slowedSomeFunction = slower(alert, 5); 

slowedSomeFunction('You will get you result in 5 seconds');

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this :

function someFunction(a, b) {
    console.log(a + b);
}

function slower(func, seconds) {
    return function() {
        console.log("You will get your result in " + seconds + " seconds");
        setTimeout(() => func.apply(this, arguments), seconds * 1000);
    }
}

let slowedSomeFunction = slower(someFunction, 5);
slowedSomeFunction(2, 3);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading