Day 7: Once

Dharan Ganesan - Jul 18 '23 - - Dev Community

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

Enter fullscreen mode Exit fullscreen mode

🤫 Check the comment below to see answer.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player