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 238 views
1 answer 220 views
1 answer 448 views
2 answers 200 views
200 views asked Feb 29, 2020 by avibootz
1 answer 116 views
1 answer 151 views
...