# listener-collection

> node.js library adding async support to events

Latest version **2.1.0** (published 2025-11-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install listener-collection
pnpm add listener-collection
yarn add listener-collection
bun add listener-collection
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2025-11-24 |
| First published | 2015-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=22.18 |
| Dependencies | 1 |
| Unpacked size | 15.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jan Blaha |
| Maintainers | pofider, bjrmatos |
| Keywords | event, async |

## Links

- npm: https://www.npmjs.com/package/listener-collection
- Repository: https://github.com/pofider/node-listener-collection
- Homepage: https://github.com/pofider/node-listener-collection#readme
- Issues: https://github.com/pofider/node-listener-collection/issues
- npm.io page: https://npm.io/package/listener-collection

## Dependencies (1)

- [async](https://npm.io/package/async.md) 3.2.3

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2025-11-24
- 2.0.0 — 2022-04-29
- 1.2.0 — 2017-11-07
- 1.0.0 — 2017-01-20
- 0.1.0 — 2015-10-12
- 0.0.2 — 2015-10-08
- 0.0.1 — 2015-10-08

## README

# node-listener-collection

[![Build Status](https://travis-ci.org/pofider/node-listener-collection.png?branch=master)](https://travis-ci.org/pofider/node-listener-collection)

This library extends the standard event mechanism with ability to wait until all asynchronous events are processed.

> npm install listener-collection


## Callback based
```js
var ListenerCollection = require("listener-collection");

var liseners = new ListenerCollection();

listeners.add("listener1", function(param1, param2, next) {
   setTimeout(function() {
     console.log("listener");  
     next();
   }, 100);
});

listeners.add("listener2", function(param1, param2, next) {
   setTimeout(function() {
     console.log("listener2");  
     next();
   }, 200);
});

listeners.fire("A", "B", function() {
  console.log("both listeners are processed now");
});
```

## Promise based
```js
var ListenerCollection = require("listener-collection");

var liseners = new ListenerCollection();

listeners.add("a name", function(param1, param2) {
   console.log("listener catch");  
   return promise;   
});

listeners.fire("A", "B").then(function() {
  console.log("everything is done");
});
```

## Listener context

```js
listeners.add("a name", context, function() {
   //this runs bound to context
});
```

## Listener position

Listeners are executed one by one in order they were added.  If you want to add a listener to a particular position you can use `insert` and specify the condition when the listener should be invoked.

```js
listeners.add("listener1", function() { ... });
listeners.add("listener2", function() { ... });
listeners.insert({ after: "listener1" }, "listener3", function() { ... });
```
Alternatively you can also use `before` instead or together with `after`. 

## Removing listener

```js
listeners.add("test", function () { });
listeners.remove("test");
```


## Hooks


```js
listeners.post(function() {
  console.log("this runs after the listeners are invoked");
});

listeners.pre(function() {
  console.log("this runs before the listeners are invoked");
});

listeners.postFail(function(err) {
  console.log("this runs after the listeners are invoked when one of the listeners fails");
});
```

## License 
MIT

---
_Source: https://npm.io/package/listener-collection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
