Gathering detailed insights and metrics for @tootallnate/once
Gathering detailed insights and metrics for @tootallnate/once
Gathering detailed insights and metrics for @tootallnate/once
Gathering detailed insights and metrics for @tootallnate/once
Creates a Promise that waits for a single event
npm install @tootallnate/once
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.8
Supply Chain
97.5
Quality
75.6
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
5,120,098,892
Last Day
1,548,754
Last Week
31,197,867
Last Month
125,387,830
Last Year
1,466,205,869
MIT License
24 Stars
20 Commits
5 Forks
2 Watchers
1 Branches
3 Contributors
Updated on Sep 17, 2024
Latest Version
3.0.0
Package Id
@tootallnate/once@3.0.0
Size
4.34 kB
NPM Version
6.14.15
Node Version
12.22.6
Published on
Sep 27, 2021
Cumulative downloads
Total Downloads
Last Day
-5%
1,548,754
Compared to previous day
Last Week
9.2%
31,197,867
Compared to previous week
Last Month
-6%
125,387,830
Compared to previous month
Last Year
-1.5%
1,466,205,869
Compared to previous year
7
Install with npm
:
1$ npm install @tootallnate/once
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.
1import once from '@tootallnate/once'; 2import { EventEmitter } from 'events'; 3 4const emitter = new EventEmitter(); 5 6setTimeout(() => { 7 emitter.emit('foo', 'bar'); 8}, 100); 9 10const [result] = await once(emitter, 'foo'); 11console.log({ result }); 12// { result: 'bar' }
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:
1const [code] = await once(process, 'exit'); 2// ^ number
A child process "exit" event contains either an exit code or a signal:
1const child = spawn('echo', []); 2const [code, signal] = await once(child, 'exit'); 3// ^ number | null 4// ^ string | null
A forked child process "message" event is type any
, so you can cast the Promise directly:
1const child = fork('file.js'); 2 3// With `await` 4const [message, _]: [WorkerPayload, unknown] = await once(child, 'message'); 5 6// With Promise 7const messagePromise: Promise<[WorkerPayload, unknown]> = once(child, 'message'); 8 9// Better yet would be to leave it as `any`, and validate the payload 10// 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:
1interface CustomEmitter extends EventEmitter { 2 on(name: 'foo', listener: (a: string, b: number) => void): this; 3} 4 5const emitter: CustomEmitter = new EventEmitter(); 6 7// "foo" event is a defined overload, so it's properly typed 8const fooPromise = once(emitter, 'foo'); 9// ^ Promise<[a: string, b: number]> 10 11// "bar" event in not a defined overload, so it gets `unknown[]` 12const barPromise = once(emitter, 'bar'); 13// ^ Promise<unknown[]>
signal
- AbortSignal
instance to unbind event handlers before the Promise has been fulfilled.No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 2/20 approved changesets -- score normalized to 1
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More