# EE First

[![NPM version](https://img.shields.io/npm/v/ee-first.svg?style=flat-square)](https://npmjs.org/package/ee-first) [![Build status](https://img.shields.io/travis/jonathanong/ee-first.svg?style=flat-square)](https://travis-ci.org/jonathanong/ee-first) [![Test coverage](https://img.shields.io/coveralls/jonathanong/ee-first.svg?style=flat-square)](https://coveralls.io/r/jonathanong/ee-first?branch=master) [![License](http://img.shields.io/npm/l/ee-first.svg?style=flat-square)](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/ee-first/LICENSE.md) [![Downloads](http://img.shields.io/npm/dm/ee-first.svg?style=flat-square)](https://npmjs.org/package/ee-first) [![Gittip](https://img.shields.io/gittip/jonathanong.svg?style=flat-square)](https://www.gittip.com/jonathanong/)

Get the first event in a set of event emitters and event pairs, then clean up after itself.

## Install

```
$ npm install ee-first
```

## API

```js
var first = require('ee-first')
```

### first(arr, listener)

Invoke `listener` on the first event from the list specified in `arr`. `arr` is an array of arrays, with each array in the format `[ee, ...event]`. `listener` will be called only once, the first time any of the given events are emitted. If `error` is one of the listened events, then if that fires first, the `listener` will be given the `err` argument.

The `listener` is invoked as `listener(err, ee, event, args)`, where `err` is the first argument emitted from an `error` event, if applicable; `ee` is the event emitter that fired; `event` is the string event name that fired; and `args` is an array of the arguments that were emitted on the event.

```js
var ee1 = new EventEmitter()
var ee2 = new EventEmitter()

first([
  [ee1, 'close', 'end', 'error'],
  [ee2, 'error']
], function (err, ee, event, args) {
  // listener invoked
})
```

#### .cancel()

The group of listeners can be cancelled before being invoked and have all the event listeners removed from the underlying event emitters.

```js
var thunk = first([
  [ee1, 'close', 'end', 'error'],
  [ee2, 'error']
], function (err, ee, event, args) {
  // listener invoked
})

// cancel and clean up
thunk.cancel()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bryan-guner.gitbook.io/my-docs/redux/repos/examples/real-world/node_modules/ee-first.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
