# @tootallnate/once

> Creates a Promise that waits for a single event

Latest version **3.0.1** (published 2026-02-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tootallnate/once
pnpm add @tootallnate/once
yarn add @tootallnate/once
bun add @tootallnate/once
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2026-02-09 |
| First published | 2020-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 10 |
| Dependencies | 0 |
| Unpacked size | 25.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 27 |
| Author | Nathan Rajlich |
| Maintainers | tootallnate |

## Links

- npm: https://www.npmjs.com/package/@tootallnate/once
- Repository: https://github.com/TooTallNate/once
- Homepage: https://github.com/TooTallNate/once#readme
- Issues: https://github.com/TooTallNate/once/issues
- npm.io page: https://npm.io/package/@tootallnate/once

## Recent versions

- 3.0.1 (latest) — 2026-02-09
- 2.0.1 (legacy) — 2026-05-03
- 3.0.0 — 2021-09-27
- 2.0.0 — 2021-09-22
- 1.1.2 — 2020-04-21
- 1.1.1 — 2020-04-20
- 1.1.0 — 2020-04-20
- 1.0.0 — 2020-02-08

## README

# @tootallnate/once

### Creates a Promise that waits for a single event

## Installation

Install with `npm`:

```bash
$ npm install @tootallnate/once
```

## API

### once(emitter: EventEmitter, name: string, opts?: OnceOptions): Promise&lt;[...Args]&gt;

Creates a Promise that waits for event `name` to occur on `emitter`, and resolves
the promise with an array of the values provided to the event handler. If an
`error` event occurs before the event specified by `name`, then the Promise is
rejected with the error argument.

```typescript
import once from '@tootallnate/once';
import { EventEmitter } from 'events';

const emitter = new EventEmitter();

setTimeout(() => {
    emitter.emit('foo', 'bar');
}, 100);

const [result] = await once(emitter, 'foo');
console.log({ result });
// { result: 'bar' }
```

#### Promise Strong Typing

The main feature that this module provides over other "once" implementations is that
the Promise that is returned is _**strongly typed**_ based on the type of `emitter`
and the `name` of the event. Some examples are shown below.

_The process "exit" event contains a single number for exit code:_

```typescript
const [code] = await once(process, 'exit');
//     ^ number
```
_A child process "exit" event contains either an exit code or a signal:_

```typescript
const child = spawn('echo', []);
const [code, signal] = await once(child, 'exit');
//     ^ number | null
//           ^ string | null
```

_A forked child process "message" event is type `any`, so you can cast the Promise directly:_

```typescript
const child = fork('file.js');

// With `await`
const [message, _]: [WorkerPayload, unknown] = await once(child, 'message');

// With Promise
const messagePromise: Promise<[WorkerPayload, unknown]> = once(child, 'message');

// Better yet would be to leave it as `any`, and validate the payload
// at runtime with i.e. `ajv` + `json-schema-to-typescript`
```

_If the TypeScript definition does not contain an overload for the specified event name, then the Promise will have type `unknown[]` and your code will need to narrow the result manually:_

```typescript
interface CustomEmitter extends EventEmitter {
    on(name: 'foo', listener: (a: string, b: number) => void): this;
}

const emitter: CustomEmitter = new EventEmitter();

// "foo" event is a defined overload, so it's properly typed
const fooPromise = once(emitter, 'foo');
//    ^ Promise<[a: string, b: number]>

// "bar" event in not a defined overload, so it gets `unknown[]`
const barPromise = once(emitter, 'bar');
//    ^ Promise<unknown[]>
```

### OnceOptions

-   `signal` - `AbortSignal` instance to unbind event handlers before the Promise has been fulfilled.

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