Question:
Write a function called once
that takes a function as an argument and returns a new function. The new function should only call the original function once and return its result for subsequent calls.
function once(fn) {
// Todo add your code
}
function expensiveOperation() {
// Time-consuming calculation
console.log("Executing expensive operation...");
return "Result";
}
const executeOnce = once(expensiveOperation);
console.log(executeOnce()); // Result
console.log(executeOnce()); // Result
🤫 Check the comment below to see answer.
Top comments (1)