1. const EventEmitter = require('events');
    2. class MyEmitter extends EventEmitter {}
    3. const myEmitter = new MyEmitter();
    4. myEmitter.on('event', () => {
    5. console.log('an event occurred!');
    6. });
    7. myEmitter.on('click', () => {
    8. console.log('on click');
    9. });
    10. myEmitter.on('error', (err) => {
    11. console.log(err);
    12. });
    13. myEmitter.emit('event');
    14. myEmitter.emit('click');
    15. myEmitter.emit('error',new Error("it is error"));

    once

    1. const EventEmitter = require('events');
    2. class MyEmitter extends EventEmitter {}
    3. const myEmitter = new MyEmitter();
    4. myEmitter.once('event', () => {
    5. console.log('an event occurred!');
    6. });
    7. setInterval(()=>{
    8. myEmitter.emit("event")
    9. })