Use EventEmitter in ES6 class

后端 未结 2 1450
栀梦
栀梦 2021-02-18 13:07

I am trying to get the EventEmitter in my own class running in ES6:

\"use strict\";
const EventEmitter = require(\'events\');

class Client extends EventEmitter{         


        
2条回答
  •  隐瞒了意图╮
    2021-02-18 13:52

    You could use process.nextTick() to make you code asynchronous. Afterwards everything will work as expected. This is the note from the node documentation:

    The process.nextTick() method adds the callback to the "next tick queue". Once the current turn of the event loop turn runs to completion, all callbacks currently in the next tick queue will be called.

    eventTest(){
        process.nextTick(() => {
            this.emit("event");
        });
        console.log(this.token);
    }
    

提交回复
热议问题