question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node.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 for events-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 to tests/index.js. Since this test will require Promise support, it should only be executed if Promises are available. See the require('./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 in README.md.

We can then release this as a minor version.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
vweeverscommented, Apr 28, 2019

You can pretty much copy https://github.com/nodejs/node/pull/26078/files, there have been no changes to the once function since that PR

1reaction
vweeverscommented, Sep 16, 2019

It really should have gotten a distinct name 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found