Big update!Introducing GitHub Bot Commands. Learn more
Socket
BlogLoveLog in
Book a demo

@tootallnate/once

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Issues
File Explorer

Advanced tools

@tootallnate/once

Creates a Promise that waits for a single event

    3.0.0latest

Version published
Maintainers
1
Weekly downloads
24,502,255
increased by2.79%

Weekly downloads

Changelog

Source

Major Changes

  • Output as ESM: #6

Patches

  • Remove as cast: a6a69bc2d24e1584b75ee00bfad94405ddaf8bcc

Readme

Source

@tootallnate/once

Creates a Promise that waits for a single event

Installation

Install with npm:

$ npm install @tootallnate/once

API

once(emitter: EventEmitter, name: string, opts?: OnceOptions): Promise<[...Args]>

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.

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:

const [code] = await once(process, 'exit'); // ^ number

A child process "exit" event contains either an exit code or a signal:

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:

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:

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.

FAQs

What is @tootallnate/once?

Creates a Promise that waits for a single event

Is @tootallnate/once popular?

The npm package @tootallnate/once receives a total of 23,511,014 weekly downloads. As such, @tootallnate/once popularity was classified as popular.

Is @tootallnate/once well maintained?

We found that @tootallnate/once demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.

Last updated on 27 Sep 2021
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc