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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to use class with event in Node.js

1 Answer

0 votes
const EventEmitter = require('events');
const uuid = require('uuid');

class CID extends EventEmitter {
    log(msg) {
        this.emit('message', { id: uuid.v4(), msg:msg })
    }
}

const cid = new CID();

cid.on('message', (data) => console.log('Data:',  data));

cid.log('Node.js');
cid.log('C++');



/*
run:
     
Data: { id: '2bc104b3-bd64-4bae-9d86-a324b7b20b58', msg: 'Node.js' }
Data: { id: 'fcdcd8f5-5e3b-4a91-a865-d253370147af', msg: 'C++' }
   
*/

 



answered Mar 10, 2020 by avibootz

Related questions

3 answers 219 views
1 answer 209 views
1 answer 435 views
2 answers 177 views
177 views asked Feb 29, 2020 by avibootz
1 answer 97 views
1 answer 141 views
...