nodejs once module

The main function of the module once module is to make the callback only execute once, which is very suitable for functions that only need to be executed once
  var wrappy = require('wrappy')
module.exports = wrappy(once)
//Define a once method on the prototype of the function after executing it once,
once.proto = once(function () {
  Object.defineProperty(Function.prototype, 'once', {
    value: function () {
      return once(this)
    },
    configurable: true
  })
})

function once (fn) {
  var f = function () {
// If you run it again, return the last return value of cb.
    if (f.called) return f.value
    f.called = true
    return f.value = fn.apply(this, arguments)
  }
  f.called = false
  return f
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327043726&siteId=291194637