Add events.once
See original GitHub issueNode.js 11.13 added an EventEmitter.once
method: https://nodejs.org/api/events.html#events_events_once_emitter_name
It returns a Promise that resolves when the requested event is fired. If an ‘error’ event is fired first, the Promise rejects with the error.
The path to implementing this looks a bit like:
- Finding the Node.js implementation (start in
lib/events.js
in the nodejs/node repository) - Finding the Node.js tests for this method. Test files are normally named something like
test-modulename-functionname.js
so you can find them by searching forevents-once
via the Github UI https://github.com/nodejs/node/find/master - Copy-paste the test file from Node.js into
tests/
and add its require call totests/index.js
. Since this test will require Promise support, it should only be executed if Promises are available. See therequire('./symbols.js')
call for one approach. (We run browser tests in old IE etc, so Promise support isn’t guaranteed.) - Copy-paste the implementation code into
events.js
at the correct location (it should be in the same order in the file as in Node.js, to make things easy to cross-reference). - Port everything to ES5…! It’s okay for the method itself to fail if Promise doesn’t exist. It should still be possible to use all other
events
features in environments that do not support Promises, though. - Document that Promise support or a global polyfill are required for the
EventEmitter.once
API to work inREADME.md
.
We can then release this as a minor version.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:9 (3 by maintainers)
Top Results From Across the Web
addEventListener "once" - DEV Community
If you want to add an event callback but have it run only once, you can simply use the once option in the...
Read more >EventTarget.addEventListener() - Web APIs | MDN
It allows adding more than one handler for an event. This is particularly useful for libraries, JavaScript modules, or any other kind of ......
Read more >How to ensure an event listener is only fired once in JavaScript
We can pass an object as an argument to the addEventListener method and specify that the event is only handled once. This is...
Read more >How can I add an event for a one time click to a function?
I am writing a function to draw a rectangle, first with one click on a button to initiate the rectangle function. Then there...
Read more >Once upon an event listener - Chrome Developers
addEventListener now supports a once option, making it easier to define events that clean up after themselves.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can pretty much copy https://github.com/nodejs/node/pull/26078/files, there have been no changes to the
once
function since that PRIt really should have gotten a distinct name 😄