25

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

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

class Client extends EventEmitter{

    constructor(token, client_id, client_secret, redirect_uri, code){
        super();
        this.token = token;
        this.client_id = client_id;
        this.client_secret = client_secret;
        this.redirect_uri = redirect_uri;
        this.code = code;
    }

    eventTest(){
        this.emit("event");
        console.log(this.token);
    }
}

let testClient = new Client(1,2,3,4,5);

testClient.eventTest();
testClient.on('event', () => {console.log('triggerd!')} );

but the event is doing nothing ^^

Without ES6 i got it working with this code:

var util = require('util');
var EventEmitter = require('events').EventEmitter;

var Client = function(credentials) {
    var self = this;

    function eventTest() {
        self.emit('event');
    }
};

util.inherits(Client, EventEmitter);

Does someone know how to do it right in ES6?

Alaska
  • 309
  • 1
  • 4
  • 9
  • 1
    Shouldn't it be `const EventEmitter = require('events').EventEmitter;`? – Johannes Reuter Jul 09 '16 at 10:31
  • 1
    [here's a bunch of good examples](https://gist.github.com/gdi2290/bec4889e0a5785cae2a9), or specifically [this one](https://gist.github.com/gdi2290/bec4889e0a5785cae2a9#file-pseudo-classical-es6-js) – nem035 Jul 09 '16 at 10:32
  • 1
    @nem035 I already found the collection, but I didn't understood how to make an event. The examples just show how to create a function inside a class :/ – Alaska Jul 09 '16 at 10:36

2 Answers2

33

Events are synchronous - you're firing it before you are listening. Use

const testClient = new Client(1,2,3,4,5);
testClient.on('event', () => {console.log('triggered!')} );
testClient.eventTest();
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks a lot! This is such a dumb mistake. – Alaska Jul 09 '16 at 10:45
  • 2
    @Alaska The occurrence of events is usually non-deterministic and thus asynchronous, unless your code emits them itself. So I wouldn't call it a dumb mistake :D –  Jul 09 '16 at 17:31
  • bergi's answer works. Just wanted to add that using an EventEmitter for synchronous code can be considered an anti-pattern. The nature of EventEmitter lies in its ability to deal with asynchronous events. Like user3674318 mentioned, consider making your method asynchronous so that the user of the abstraction can use it as expected. [Node.js Design Patterns Chapter 3][1] is a terrific resource for understanding the Observer pattern. [1]: https://www.nodejsdesignpatterns.com/ – Nathan Jun 20 '21 at 06:03
  • Is there a way to do this in the browser w/o a library? – chovy Nov 09 '22 at 07:47
  • @chovy If you mean using the nodejs `EventEmitter`, no that is not possible – Bergi Nov 09 '22 at 09:55
3

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);
}
Axel
  • 3,331
  • 11
  • 35
  • 58