Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master

once@1.3.2

Swtich from BSD to ISC licenses, and in the process adopt an
SPDX-compliant license string.
latest commit e9a933a914
@othiym23 othiym23 authored
..
Failed to load latest commit information.
test once@1.3.1
LICENSE once@1.3.2
README.md once@1.3.0
once.js once@1.3.1
package.json once@1.3.2

README.md

once

Only call a function once.

usage

var once = require('once')

function load (file, cb) {
  cb = once(cb)
  loader.load('file')
  loader.once('load', cb)
  loader.once('error', cb)
}

Or add to the Function.prototype in a responsible way:

// only has to be done once
require('once').proto()

function load (file, cb) {
  cb = cb.once()
  loader.load('file')
  loader.once('load', cb)
  loader.once('error', cb)
}

Ironically, the prototype feature makes this module twice as complicated as necessary.

To check whether you function has been called, use fn.called. Once the function is called for the first time the return value of the original function is saved in fn.value and subsequent calls will continue to return this value.

var once = require('once')

function load (cb) {
  cb = once(cb)
  var stream = createStream()
  stream.once('data', cb)
  stream.once('end', function () {
    if (!cb.called) cb(new Error('not found'))
  })
}
Something went wrong with that request. Please try again.