Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,111 questions

40,781 answers

573 users

How to create and fire event with arguments in Node.js

3 Answers

0 votes
const events = require('events');
const eventEmitter = new events.EventEmitter();
 
// Register (add) a listener
eventEmitter.on('gonodego', function(arg) {
    console.log(arg);
});
 
// Fire (Raise) an event with arguments:
eventEmitter.emit('gonodego', { id: 88622, name: 'Elizabeth' });
   
  
   
/*
run:
   
{ id: 88622, name: 'Elizabeth' }
   
*/

 





answered Mar 2, 2020 by avibootz
0 votes
const events = require('events');
const eventEmitter = new events.EventEmitter();
 
// Register (add) a listener
eventEmitter.on('gonodego', arg => {
    console.log(arg);
});
 
// Fire (Raise) an event with arguments:
eventEmitter.emit('gonodego', { id: 88622, name: 'Elizabeth' });
   
  
   
/*
run:
   
{ id: 88622, name: 'Elizabeth' }
   
*/

 





answered Mar 2, 2020 by avibootz
0 votes
// The module: log.js

const events = require('events');

class Log extends events {
   show(s) {
     console.log(s);
  
     // Fire (Raise) an event with arguments:
     this.emit('gonodego', { id: 88622, name: 'Elizabeth' });
  }
}

module.exports = Log;
// The application: app.js

const Log = require('./log');
const log = new Log();   

// Register (add) a listener
log.on('gonodego', arg => {
    console.log(arg);
});

log.show('node.js');




/*
run:
   
node.js
{ id: 88622, name: 'Elizabeth' }
   
*/

 





answered Mar 2, 2020 by avibootz
edited Mar 2, 2020 by avibootz

Related questions

2 answers 87 views
1 answer 149 views
1 answer 84 views
84 views asked Mar 10, 2020 by avibootz
1 answer 342 views
...