npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-once-events

v0.2.0

Published

Return promise for events.once method

Downloads

3

Readme

promise-once-events

Build Status Coverage Status npm

This module provides promisified version of standard EventEmitter class, except that once method returns Promise object which is fulfilled when emit method is called.

Requirements

This module requires Node >= 4. For Node < 6 --harmony flag is required.

Installation

npm install promise-once-events

Usage

promise-once-events can be a base class for custom event emitter.

const PromiseOnceEvents = require('promise-once-events')

class MyEmitter extends PromiseOnceEvents {}

const emitter = new MyEmitter()

Method once returns Promise object which is fulfilled when emit method is called and then result is an Arguments object which contains arguments from emit method.

// As promise
emitter.once('event').then(result => {
  // result is object Arguments
  console.log('an event occurred with arguments:', result)
})

emitter.emit('event', 'a', 'b')

It also works with async/await syntax:

// As promise
const result = await emitter.once('event')
// result is object Arguments
console.log('an event occurred with arguments:', result)

If the last argument for once method is a callback then it works as for original EventEmitter.once method.

// With callback
emitter.once('event', (a, b) => {
  console.log('an event occurred with arguments:', [a, b])
})

emitter.emit('event', 'a', 'b')

Overriding EventEmitter

PromiseOnceEvents can be used as a replacement for EventEmitter for existing objects.

Example:

const readable = fs.createReadStream(process.argv[2] || __filename)

readable.once = PromiseOnceEvents.prototype.once

readable.on('data', (chunk) => {
  console.log(`Received ${chunk.length} bytes of data.`)
})

await readable.once('end')
console.log('There will be no more data.')

Promise

This module uses any-promise and any ES6 Promise library or polyfill is supported.

Ie. bluebird can be used as Promise library for this module, if it is registered before.

require('any-promise/register/bluebird')
const PromiseOnceEvents = require('promise-once-events')

License

Copyright (c) 2016-2017 Piotr Roszatycki [email protected]

MIT