1.2.0 • Published 8 years ago

load-once v1.2.0

Weekly downloads
3
License
ISC
Repository
github
Last release
8 years ago

load-once

Pass a function to load-once and it'll only get executed once, sort of like a singleton.

// foo.js
var loadOnce = require('load-once')
var value = 0

function operation (callback) {
  value++

  callback(null, value)
}

module.exports = loadOnce(operation)

// bar.js
var foo = require('foo.js')

foo(function (error, result) {
  console.info(result) // prints '1'
})

// baz.js
var foo = require('foo.js')

foo(function (error, result) {
  console.info(result) // prints '1'
})

Arguments are also supported:

// foo.js
var loadOnce = require('load-once')

function operation (arg1, arg2, callback) {
  callback(null, arg1 + ' ' + arg2)
}

module.exports = loadOnce(operation)

// bar.js
var foo = require('foo.js')

foo('hello', 'world', function (error, result) {
  console.info(result) // prints 'hello world'
})
1.2.0

8 years ago

1.1.0

8 years ago

1.0.1

9 years ago

1.0.0

9 years ago