Gathering detailed insights and metrics for p-event
Gathering detailed insights and metrics for p-event
Gathering detailed insights and metrics for p-event
Gathering detailed insights and metrics for p-event
npm install p-event
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.6
Supply Chain
99.5
Quality
75.9
Maintenance
100
Vulnerability
100
License
JavaScript (83.15%)
TypeScript (16.85%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
435 Stars
59 Commits
25 Forks
8 Watchers
1 Branches
17 Contributors
Updated on Apr 22, 2025
Latest Version
6.0.1
Package Id
p-event@6.0.1
Unpacked Size
24.08 kB
Size
6.11 kB
File Count
5
NPM Version
9.2.0
Node Version
21.6.2
Published on
Mar 02, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Promisify an event by waiting for it to be emitted
Useful when you need only one event emission and want to use it with promises or await it in an async function.
It works with any event API in Node.js and the browser (using a bundler).
If you want multiple individual events as they are emitted, you can use the pEventIterator()
method. Observables can be useful too.
1npm install p-event
In Node.js:
1import {pEvent} from 'p-event'; 2import emitter from './some-event-emitter'; 3 4try { 5 const result = await pEvent(emitter, 'finish'); 6 7 // `emitter` emitted a `finish` event 8 console.log(result); 9} catch (error) { 10 // `emitter` emitted an `error` event 11 console.error(error); 12}
In the browser:
1import {pEvent} from 'p-event'; 2 3await pEvent(document, 'DOMContentLoaded'); 4console.log('😎');
Async iteration:
1import {pEventIterator} from 'p-event'; 2import emitter from './some-event-emitter'; 3 4const asyncIterator = pEventIterator(emitter, 'data', { 5 resolutionEvents: ['finish'] 6}); 7 8for await (const event of asyncIterator) { 9 console.log(event); 10}
Returns a Promise
that is fulfilled when emitter
emits an event matching event
, or rejects if emitter
emits any of the events defined in the rejectionEvents
option.
Note: event
is a string for a single event type, for example, 'data'
. To listen on multiple
events, pass an array of strings, such as ['started', 'stopped']
.
The returned promise has a .cancel()
method, which when called, removes the event listeners and causes the promise to never be settled.
Type: object
Event emitter object.
Should have either a .on()
/.addListener()
/.addEventListener()
and .off()
/.removeListener()
/.removeEventListener()
method, like the Node.js EventEmitter
and DOM events.
Type: string | string[]
Name of the event or events to listen to.
If the same event is defined both here and in rejectionEvents
, this one takes priority.
Type: object
Type: string[]
Default: ['error']
Events that will reject the promise.
Type: boolean
Default: false
By default, the promisified function will only return the first argument from the event callback, which works fine for most APIs. This option can be useful for APIs that return multiple arguments in the callback. Turning this on will make it return an array of all arguments from the callback, instead of just the first argument. This also applies to rejections.
Example:
1import {pEvent} from 'p-event'; 2import emitter from './some-event-emitter'; 3 4const [foo, bar] = await pEvent(emitter, 'finish', {multiArgs: true});
Type: number
Default: Infinity
Time in milliseconds before timing out.
Type: Function
A filter function for accepting an event.
1import {pEvent} from 'p-event'; 2import emitter from './some-event-emitter'; 3 4const result = await pEvent(emitter, '🦄', value => value > 3); 5// Do something with first 🦄 event with a value greater than 3
Type: AbortSignal
An AbortSignal
to abort waiting for the event.
Wait for multiple event emissions. Returns an array.
This method has the same arguments and options as pEvent()
with the addition of the following options:
Type: object
Required
Type: number
The number of times the event needs to be emitted before the promise resolves.
Type: boolean
Default: false
Whether to resolve the promise immediately. Emitting one of the rejectionEvents
won't throw an error.
Note: The returned array will be mutated when an event is emitted.
Example:
1import {pEventMultiple} from 'p-event'; 2 3const emitter = new EventEmitter(); 4 5const promise = pEventMultiple(emitter, 'hello', { 6 resolveImmediately: true, 7 count: Infinity 8}); 9 10const result = await promise; 11console.log(result); 12//=> [] 13 14emitter.emit('hello', 'Jack'); 15console.log(result); 16//=> ['Jack'] 17 18emitter.emit('hello', 'Mark'); 19console.log(result); 20//=> ['Jack', 'Mark'] 21 22// Stops listening 23emitter.emit('error', new Error('😿')); 24 25emitter.emit('hello', 'John'); 26console.log(result); 27//=> ['Jack', 'Mark']
Returns an async iterator that lets you asynchronously iterate over events of event
emitted from emitter
. The iterator ends when emitter
emits an event matching any of the events defined in resolutionEvents
, or rejects if emitter
emits any of the events defined in the rejectionEvents
option.
This method has the same arguments and options as pEvent()
with the addition of the following options:
Type: object
Type: number
(non-negative integer)
Default: Infinity
The maximum number of events for the iterator before it ends. When the limit is reached, the iterator will be marked as done
. This option is useful to paginate events, for example, fetching 10 events per page.
Type: string[]
Default: []
Events that will end the iterator.
Exposed for instance checking and sub-classing.
Example:
1import {pEvent} from 'p-event'; 2 3try { 4 await pEvent(emitter, 'finish'); 5} catch (error) { 6 if (error instanceof pEvent.TimeoutError) { 7 // Do something specific for timeout errors 8 } 9}
1import fs from 'node:fs'; 2 3function getOpenReadStream(file, callback) { 4 const stream = fs.createReadStream(file); 5 6 stream.on('open', () => { 7 callback(null, stream); 8 }); 9 10 stream.on('error', error => { 11 callback(error); 12 }); 13} 14 15getOpenReadStream('unicorn.txt', (error, stream) => { 16 if (error) { 17 console.error(error); 18 return; 19 } 20 21 console.log('File descriptor:', stream.fd); 22 stream.pipe(process.stdout); 23});
1import fs from 'node:fs'; 2import {pEvent} from 'p-event'; 3 4async function getOpenReadStream(file) { 5 const stream = fs.createReadStream(file); 6 await pEvent(stream, 'open'); 7 return stream; 8} 9 10(async () => { 11 const stream = await getOpenReadStream('unicorn.txt'); 12 console.log('File descriptor:', stream.fd); 13 stream.pipe(process.stdout); 14})() 15 .catch(console.error);
Some functions might use a single event for success and for certain errors. Promises make it easy to have combined error handler for both error events and successes containing values which represent errors.
1import {pEvent} from 'p-event'; 2import emitter from './some-event-emitter'; 3 4try { 5 const result = await pEvent(emitter, 'finish'); 6 7 if (result === 'unwanted result') { 8 throw new Error('Emitter finished with an error'); 9 } 10 11 // `emitter` emitted a `finish` event with an acceptable value 12 console.log(result); 13} catch (error) { 14 // `emitter` emitted an `error` event or 15 // emitted a `finish` with 'unwanted result' 16 console.error(error); 17}
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 11/30 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
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
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