Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: how can I return a value from an event listener?

How to i return value from the an event listeners?

See example below:

const EventEmitter = require("events").EventEmitter;
emitter = new EventEmitter();
emitter.on("sayHello", function(message) {
    return message + " World";
});
let helloMessage = emitter.emit("sayHello", "Hello");
console.log(helloMessage); // It should output: "Hello World"

I want to modify event value and return the modified version.

How do i do it?

like image 895
Empire Avatar asked Mar 15 '17 06:03

Empire


People also ask

Can you return a value from an event listener?

Event listeners return nothing by default, but you can get data out of them in clever ways. This MDN article described 3 methods: Getting data into an event listener using "this" Getting data into an event listener using the outer scope property.

What is the return type of eventEmitter listeners event )?

The return value of event handlers is completely ignored. From the documentation: When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded.

What is used to returning from event emitter object?

Finally, we return an object of EventEmitter from the function. So now, we can use the return value of LoopProcessor function to bind these events using on() or addListener() function.

How do you find the event listener of an element?

Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element. You can expand any event listener by clicking the right-pointing arrowhead.


1 Answers

The return value of event handlers is completely ignored. From the documentation:

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded.

But what you can do instead is emit an object and have the callback modify the state of that object:

const EventEmitter = require("events").EventEmitter;
const emitter = new EventEmitter();
emitter.on("sayHello", function(e) {
    e.message += " World";              // Modifying the state
});
const e = {message: "Hello"};
emitter.emit("sayHello", e);
console.log(e.message); // "Hello World"

(Also note the changes creating an instance of EventEmitter; your original code tried to use on on EventEmitter itself, but it doesn't have one.)


You've said in the comments that you would like to avoid having to create the object as its own step. You can easily subclass EventEmitter to add your own emitObject (or whatever) function:

class MyEventEmitter extends EventEmitter {
    emitObject(event, obj = {}) {
        this.emit(event, obj);
        return obj;
    }
}

Usage:

const emitter = new MyEventEmitter();
emitter.on("sayHello", function(e) {
    e.message += " World";
});
const evt = emitter.emitObject("sayHello", {message: "Hello"});
console.log(evt.message); // "Hello World"

I've supplied an object for the second argument there (so we could do the "Hello" + " World" thing), but if you left it off, a blank object would be defaulted for it (ES2015 default arguments).

Complete example:

const EventEmitter = require("events").EventEmitter;
class MyEventEmitter extends EventEmitter {
    emitObject(event, obj = {}) {
        this.emit(event, obj);
        return obj;
    }
}
const emitter = new MyEventEmitter();
emitter.on("sayHello", function(e) {
    e.message += " World";
});
const evt = emitter.emitObject("sayHello", {message: "Hello"});
console.log(evt.message); // "Hello World"
like image 158
T.J. Crowder Avatar answered Sep 28 '22 00:09

T.J. Crowder