How to write a function that returns a callback, but only the result of the first time calling the callback.
Returns a function that is restricted to invoking func once. Repeat calls to the function return the value of the first call.
function once(func) {
let result = func
let counter = 0
//let result
function inner (n) {
counter++
if (counter = 1) {
return func
}
if (counter > 1) {
return result
}
}
return inner
}
I can't put my finger on what I'm doing wrong.