how to write a function once that accepts a callback as input and returns a function?
guys, i need a bit of help on the problem:
Write a function once that accepts a callback as input and returns a function. When the returned function is called the first time, it should call the callback and return that output. If it is called any additional times, instead of calling the callback again it will simply return the output value from the first time it was called.
i dont all the rest but stuck on on can i return the value of when the first call back was first call how can i store it even
Write a function once that accepts a callback as input and returns a function. When the returned function is called the first time, it should call the callback and return that output. If it is called any additional times, instead of calling the callback again it will simply return the output value from the first time it was called.
i dont all the rest but stuck on on can i return the value of when the first call back was first call how can i store it even
No any search results
You already invited:
1 Answers
Kai
Upvotes from:
Something like this.
function fn() {The returned function closes over the called variable in the scope that it was created inlet called = false;
return () => { if (called) { return "called"} else { called = true; return "not called"; }}
}