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
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
24 Stars
20 Commits
7 Forks
3 Watching
1 Branches
3 Contributors
Updated on 17 Sept 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-8%
5,501,160
Compared to previous day
Last week
1.4%
32,322,162
Compared to previous week
Last month
11.3%
132,706,487
Compared to previous month
Last year
-0.5%
1,462,666,712
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
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
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 2024-11-25
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