simple-once
v2.0.1
Published
the simplest way to call a function once
Downloads
6
Maintainers
Readme
simple-once
the simplest way to call a function once
API
var once = require('simple-once')
once(function([args]))
Usage
var once = require('simple-once')
var fs = require('fs')
var file = fs.createReadStream('file.txt')
function log () {
console.log.apply(this, arguments)
}
var cb = once(log)
// cb will be called only once
file.on('data', cb)
file.on('end', cb)
file.on('close', cb)
file.on('error', cb)
loading a function once
var once = require('simple-once')
// file foo.js
var counter = 0
module.exports = once(foo)
function foo (cb) {
counter++
cb(null, counter)
}
// file bar.js
var foo = require('./foo')
foo(function (error, result) {
console.info(result) // should print 1
})
// file barr.js
var foo = require('./foo')
foo(function (error, result) {
console.info(result) // should print 1
})
with arguments
var once = require('simple-once')
// file foo.js
module.exports = once(foo)
function foo (message, cb) {
cb(null, message.toUpperCase() + '!')
}
// file bar.js
var foo = require('./foo')
foo('hello world', function (error, result) {
console.info(result) // should print HELLO WORLD!
})
Development
this projet has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit
to run test
npm test
to run jshint
npm run lint
to run code style
npm run style
to run check code coverage
npm run coverage:check
to open the code coverage report
npm run coverage:open