11

I have a giant function with a lot of nested callbacks. I want to make it cleaner and easier to handle. So, I'm thinking of using custom event listeners

Like, when a function is done, in the callback, instead of putting a chunk of code, it just emits an event, and then the listener will run.

So, how to do that in node.js? I am still looking for a good example to guide me through.

4
  • 2
    Related stackoverflow.com/questions/4903154/…? Commented Apr 21, 2011 at 2:12
  • Thanks. I will try. which one is the better one? Commented Apr 21, 2011 at 18:10
  • The first one is most related to your problem. Commented Apr 22, 2011 at 11:15
  • I know it's an old question, but how about async.js instead of event listeners? Commented Oct 16, 2014 at 17:48

4 Answers 4

29

You can set events like this

app.on('event:user_created', callback);

Then you can emit them

app.emit('event:user_created', data);

express.js uses EventEmitter.

Sign up to request clarification or add additional context in comments.

6 Comments

It's actually that app extends EventEmitter in connect framework which express extends . connect.js@68 utils.merge(app, EventEmitter.prototype);
So the express app can act as an app-wide dispatch
will this take an err object - app.emit('event:foo', err, data) - for instance if we're returning the result of a database query?
I don't think this works anymore with express.js 4. All I get when I call app.emit() is an undefined is not a function error
in Express.js you can do req.app.emit(...). Because every request object has a reference of the app
|
9

You probably want to create an EventEmitter object, and call emit() on it.

Comments

6

I just came across this question, I'd like to chip in my 2 cents, specifically responding to Luis.

In Express, if you want the 'app' instance to listen for a custom event you would do something like:

app.on('testEvent', function () {
  return console.log('responded to testEvent');
});

app.get('/test', function (req, res) {
  app.emit('testEvent');
  return res.status(200).end();
});

Then when that route is hit, you'd see the response in the console. Of course you can implement this as you wish.

Comments

1

The Node.js "events" module and the "EventEmitter" module facilitates communication between objects in Node. The EventEmitter module is at the core of Node's asynchronous event-driven architecture. Here is how you can create custom events and emit them:

const EventEmitter = require('events');
const myEmitter = new EventEmitter();

//Event Listener
const EventListenerFunc = () => {
 console.log('an event occurred!');
}

//Registering the event with the listener
myEmitter.on('eventName', EventListenerFunc);

//Emitting the event 
myEmitter.emit('eventName');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.