1.0.0 • Published 9 years ago
tiny-once v1.0.0
Run Function Only Once
An implementation of a wrapper to execute a function only once that is really tiny and will work on the majority of use cases.
Install
npm install tiny-onceUsage
Whenever you have a function that must be run only once, you can wrap that function with tiny-once as follows:
const once = require('tiny-once')
function mustBeRunOnlyOnce() {
// code here...
}
const wrapped = once(mustBeRunOnlyOnce)
wrapped() // executes mustBeRunOnlyOnce
wrapped() // does nothing
wrapped() // does nothing either!You should also be able to use the module in the browser through browserify or webpack!
API
once(fn[, options])
fnis the function that shall be run only once.optionsis an optional object with the following properties.contextis the object used asthiswhen callingfn.cachedis a boolean flag that makes the calls to the wrapped function to return the original call return value instead of doing nothing whenfnis called again.
License
WTFPL
1.0.0
9 years ago