Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eventemitter-callback

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventemitter-callback

Emit an event and waiting for a result.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

NPM version Downloads Package tree-shaking Package minified & gzipped size

eventemitter-callback

Emit an event and waiting for a result. Based on eventemitter3.

Installation

By package manager:

npm install eventemitter-callback --save

# For Yarn, use the command below.
yarn add eventemitter-callback

Installation from CDN:

<!-- For UNPKG use the code below. -->
<script src="https://unpkg.com/eventemitter-callback"></script>

<!-- For JSDelivr use the code below. -->
<script src="https://cdn.jsdelivr.net/npm/eventemitter-callback"></script>

<script>
  // UMD module is exposed through the "EventEmitterCB" global variable.
  console.log(EventEmitterCB);
</script>

Usage

import emitter from 'eventemitter-callback'

emitter.on('event-name', (arg) => {
  console.log(arg) // prints "ping"
  return 'pong'
})

// emit an event and waiting for its result
emitter.emit('event-name', 'ping', (arg) => {
  console.log(arg) // prints "pong"
})

Disable triggering events before listening

By default, the following works normally:

emitter.emit('msg', 1)
emitter.on('msg', (arg) => {
  console.log(arg) // prints 1
})

If this doesn't meet your expectations, you can disable it:

emitter.disablePreEmitter();
emitter.emit('msg', 1)
emitter.on('msg', (arg) => {
  console.log(arg) // will not be called!
})

API

on(eventName, listener)

Adds the listener function to the end of the listeners array for the event named eventName.

  • eventName string the name of the event
  • listener function the callback function

The listener function support AsyncFunction or returns a Promise

emitter.on('event-name', (arg) => {
  console.log(arg) // prints "ping"
  return new Promise((resolve) => {
    resolve('pong')
  })
})
// emitter.on('event-name', async (arg) => {
//   console.log(arg) // prints "ping"
//   const res = await queryRemote() // suppose res returns 'pong'
//   return res
// })
emitter.emit('event-name', 'ping', (arg) => {
  console.log(arg) // prints "pong"
})

once(eventName, listener)

  • eventName string the name of the event
  • listener function the callback function

Adds a one-time listener function for the event named eventName.

emit(eventName[, ...args][, callback])

  • eventName string the name of the event
  • ...args any
  • callback any the callback function

Returns true if the event had listeners, false otherwise.

emitAll(eventName[, ...args])

  • eventName string the name of the event
  • ...args any

Trigger all the listeners at once. Returns a result array by Promise.all.

emitValidateAll(eventName[, ...args])

  • eventName string the name of the event
  • ...args any

Trigger all the listeners at once and check the values returned. Returns true if all listeners returns true, false otherwise.

emitValidateAnyOf(eventName[, ...args])

  • eventName string the name of the event
  • ...args any

Trigger all the listeners at once and check the values returned. Returns true if either listener returns true, false otherwise.

off(eventName[, listener])

  • eventName string the name of the event
  • listener function the callback function

Removes the specified listener from the listener array for the event named eventName.

Removes all the listeners for the event:

emitter.off('event-name')

emitter._eventEmitter

Reference to current instance of EventEmitter3.

emitter._EventEmitter

Reference to constructor of EventEmitter3.

License

MIT, see the LICENSE file for detail.

Keywords

FAQs

Package last updated on 19 Jul 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc