Gathering detailed insights and metrics for event-as-promise
Gathering detailed insights and metrics for event-as-promise
Gathering detailed insights and metrics for event-as-promise
Gathering detailed insights and metrics for event-as-promise
wConsequence
Advanced synchronization mechanism. Asynchronous routines may use Consequence to wrap postponed result, what allows classify callback for such routines as output, not input, what improves analyzability of a program. Consequence may be used to make a queue
@coffeekraken/s-promise
As simple as a native Promise, this package extends it with events capabilities using the @coffeekraken/s-event-emitter one.
lt-event-promise
Same as EventEmitter but return Promise of executing listeners when emit()
event-emitter-promisify
Utility to resolve EventEmitters as a promise
Handle continuous steam of events in Promise fashion
npm install event-as-promise
Typescript
Module System
Node Version
NPM Version
89.1
Supply Chain
100
Quality
82.9
Maintenance
100
Vulnerability
100
License
TypeScript (54.93%)
JavaScript (42.95%)
HTML (2.12%)
Total Downloads
1,643,157
Last Day
2,186
Last Week
11,107
Last Month
46,099
Last Year
478,887
MIT License
3 Stars
45 Commits
1 Forks
1 Watchers
2 Branches
1 Contributors
Updated on Dec 06, 2024
Minified
Minified + Gzipped
Latest Version
2.0.0
Package Id
event-as-promise@2.0.0
Unpacked Size
17.54 kB
Size
5.21 kB
File Count
9
NPM Version
10.5.2
Node Version
20.13.1
Published on
Jun 02, 2024
Cumulative downloads
Total Downloads
Last Day
-6.3%
2,186
Compared to previous day
Last Week
-2.5%
11,107
Compared to previous week
Last Month
-4.8%
46,099
Compared to previous month
Last Year
51.9%
478,887
Compared to previous year
Handle continuous stream of events with Promise and generator function.
Instead of listen to event just once, event-as-promise
chose an approach to allow listening to the same event continuously. And we support generator function to enable for await (const data of eventAsPromise)
loop to handle event indefinitely.
We moved default imports to named imports:
1- import EventAsPromise from 'event-as-promise'; 2+ import { EventAsPromise } from 'event-as-promise';
We removed options: { array: boolean }
, to receive all arguments from Node.js event emitter:
1- target.on(eventAsPromise.eventListener); 2+ target.on((...args) => eventAsPromise.eventListener(args));
Only eventListener
is bound to the instance of EventAsPromise
. Other functions (one
and upcoming
) are not bound and will need to be call in the context of EventAsPromise
. If you want to call it bound:
1 const eventAsPromise = new EventAsPromise(); 2- const one = eventAsPromise.one; 3+ const one = eventAsPromise.one.bind(eventAsPromise) 4 5 button.addEventListener('click', eventAsPromise.eventListener); 6 7 await one();
This sample code is converted from Node about page.
1import { EventAsPromise } from 'event-as-promise'; 2import http from 'http'; 3 4async function main(ready) { 5 const server = http.createServer(); 6 const listeningPromises = new EventAsPromise(); 7 const requestPromises = new EventAsPromise(); 8 9 server 10 .once('listening', listeningPromises.eventListener) 11 .on('request', (...args) => requestPromises.eventListener(args)) 12 .listen(3000); 13 14 // Wait for "listening" 15 await listeningPromises.one(); 16 17 // Loop indefinitely, using generator 18 for (let requestPromise of requestPromises) { 19 // Wait for "request" 20 const [req, res] = await requestPromise; 21 22 res.statusCode = 200; 23 res.setHeader('Content-Type', 'text/plain'); 24 res.end('Hello World\n'); 25 } 26} 27 28main();
Handling event in a Promise may not reduce complexity. But it will be beneficial for redux-saga
when mixed with call
effect.
In this example, when the user is connected via CONNECTED
action, we will keep the user posted about file changes, until a DISCONNECTED
is received.
1saga.run(function* () { 2 yield takeLatest('CONNECTED', function* (action) { 3 const watcher = fs.watch(action.payload); 4 const changePromises = new EventAsPromise(); 5 6 watcher.on('change', changePromises.eventListener); 7 8 for (;;) { 9 const changes = yield race([ 10 call(changePromises.one), 11 take('DISCONNECTED'), 12 ]); 13 14 if (changes) { 15 yield put({ type: 'CHANGED', payload: changes }); 16 } else { 17 break; 18 } 19 } 20 21 watcher.close(); 22 }); 23});
You can retrieve multiple Promise objects before the event is emitted.
1const emitter = new EventEmitter(); 2const countPromises = new EventAsPromise(); 3 4emitter.on('count', countPromises.eventListener); 5 6// Retrieve multiple future Promise before the actual event is fired 7const promise1 = countPromises.one(); 8const promise2 = countPromises.one(); 9const promise3 = countPromises.one(); 10 11emitter.emit('count', 1); 12emitter.emit('count', 2); 13emitter.emit('count', 3); 14 15await expect(promise1).resolves.toBe(1); 16await expect(promise2).resolves.toBe(2); 17await expect(promise3).resolves.toBe(3);
Same as event listener, if
one()
is not called before the event is emitted, the event will be lost.
Instead of futures, you can use upcoming()
to get the Promise for the upcoming event. Futures and upcoming Promises are independent of each other, as shown in the sample below.
1const emitter = new EventEmitter(); 2const countPromises = new EventAsPromise(); 3 4emitter.on('count', countPromises.eventListener); 5 6const promiseOne1 = countPromises.upcoming(); 7const promiseOne2 = countPromises.upcoming(); 8const promiseOne3 = countPromises.one(); 9const promiseTwo = countPromises.one(); 10 11emitter.emit('count', 'one'); 12emitter.emit('count', 'two'); 13 14await expect(promiseOne1).resolves.toBe('one'); 15await expect(promiseOne2).resolves.toBe('one'); 16await expect(promiseOne3).resolves.toBe('one'); 17await expect(promiseTwo).resolves.toBe('two'); 18 19const promiseThree = countPromises.upcoming(); 20 21emitter.emit('count', 'three'); 22 23await expect(promiseOne1).resolves.toBe('one'); 24await expect(promiseThree).resolves.toBe('three');
Note: after the current
upcoming()
has resolved, you will need to callupcoming()
again to obtain a new Promise for the next upcoming event.
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 5
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
7 existing vulnerabilities detected
Details
Reason
Found 0/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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
Project has not signed or included provenance with any releases.
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