invoked-once
Version:
Invoke a function only once
20 lines (16 loc) • 344 B
JavaScript
module.exports = invokedOnce
function invokedOnce(fn, ctx) {
let invoked
let result
if (typeof fn !== 'function') {
throw new Error(`Function expected, but got + ${fn}`)
}
return function() {
if (invoked) {
return result
}
invoked = true
result = fn.apply(ctx || this, arguments)
return result
}
}