# race-events

> Race event emissions from a set of emitters and events.

Latest version **1.0.0** (published 2019-08-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install race-events
pnpm add race-events
yarn add race-events
bun add race-events
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-08-02 |
| First published | 2019-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ricardo Liang |
| Maintainers | rliang |
| Keywords | event-emitter, event, promise, await, race |

## Links

- npm: https://www.npmjs.com/package/race-events
- Repository: https://github.com/rliang/race-events.js
- Homepage: https://github.com/rliang/race-events.js#readme
- Issues: https://github.com/rliang/race-events.js/issues
- npm.io page: https://npm.io/package/race-events

## 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

- 1.0.0 (latest) — 2019-08-02

## README

# race-events.js
[![npm](https://img.shields.io/npm/v/race-events.svg)](https://www.npmjs.org/package/race-events)
[![size](https://img.shields.io/bundlephobia/minzip/race-events.svg)](https://bundlephobia.com)
[![deps](https://david-dm.org/rliang/race-events/status.svg)](https://david-dm.org/rliang/race-events)

Race event emissions from a set of emitters and events.

- Supports Node's `EventEmitter` and browser's `EventTarget`
- Supports Typescript

## Installation

```sh
npm i race-events
```

## API

```ts
raceEvents(...specs: [Emitter, ResolveEventName[], RejectEventName[]?][]):
  Promise<{ target: Emitter, type: ResolveEventName, data: any[] }>
```

## Usage

```ts
import r from "race-events";
import WebSocket from "ws";

type WSMessage = { target: WebSocket; type: "message"; data: [WebSocket.Data] };
type WSClose = { target: WebSocket; type: "close"; data: [number, string] };
type WSError = { target: WebSocket; type: "error"; data: [Error] };

const wss = new WebSocket.Server();

wss.on("connection", async function(ws) {
  try {

    // await messages, throw close and error events
    let a = await r<WSMessage>([ws, ["message"], ["close", "error"]]);
    console.log(a.type, a.target, a.data);

    // do not throw close and error events
    let b = await r<WSMessage | WSClose | WSError>([ws, ["message", "close", "error"]]);

  } catch (e) {
    console.error("threw", e);
  }
});
```

## Related libraries

- [ee-first](https://www.npmjs.com/package/ee-first)
- [await-first](https://www.npmjs.com/package/await-first)
- [first-event](https://www.npmjs.com/package/first-event)
- [event-race](https://www.npmjs.com/package/event-race)

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