Socket
Socket
Sign inDemoInstall

wrappy

Package Overview
Dependencies
0
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    wrappy

Callback wrapping utility


Version published
Weekly downloads
48M
decreased by-0.88%
Maintainers
2
Install size
3.60 kB
Created
Weekly downloads
 

Package description

What is wrappy?

The wrappy npm package is a simple utility module designed to wrap asynchronous functions. It ensures that the callback provided to the wrapped function is only called once, which can be useful in preventing issues where a callback might accidentally be called multiple times due to programming errors.

What are wrappy's main functionalities?

Callback wrapping

This code demonstrates how to use wrappy to wrap a callback function so that it can only be called once. The 'once' function wraps the provided function 'fn' using wrappy, ensuring that subsequent calls to 'wrapped' after the first one have no effect.

const wrappy = require('wrappy')

function once (fn) {
  return wrappy(function () {
    if (fn === null) return
    var callFn = fn
    fn = null
    callFn.apply(this, arguments)
  })
}

var wrapped = once(function (a) {
  console.log('Called with', a)
})

wrapped('first call')
wrapped('second call') // This will not be called

Other packages similar to wrappy

Readme

Source

wrappy

Callback wrapping utility

USAGE

var wrappy = require("wrappy")

// var wrapper = wrappy(wrapperFunction)

// make sure a cb is called only once
// See also: http://npm.im/once for this specific use case
var once = wrappy(function (cb) {
  var called = false
  return function () {
    if (called) return
    called = true
    return cb.apply(this, arguments)
  }
})

function printBoo () {
  console.log('boo')
}
// has some rando property
printBoo.iAmBooPrinter = true

var onlyPrintOnce = once(printBoo)

onlyPrintOnce() // prints 'boo'
onlyPrintOnce() // does nothing

// random property is retained!
assert.equal(onlyPrintOnce.iAmBooPrinter, true)

FAQs

Last updated on 17 May 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc