
Build a CRUD REST API with Node.js, Express, and PostgreSQL, then modernize it with ES modules, async/await, built-in Express middleware, and safer config handling.

Discover what’s new in The Replay, LogRocket’s newsletter for dev and engineering leaders, in the March 25th issue.

Discover a practical framework for redesigning your senior developer hiring process to screen for real diagnostic skill.

I tested the Speculation Rules API in a real project to see if it actually improves navigation speed. Here’s what worked, what didn’t, and where it’s worth using.
Hey there, want to help make our blog better?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up now
3 Replies to "Handling and dispatching events with Node.js"
Hello!
in your example:
myEmitter.on(‘ping’, function (data) {
console.log(‘First event: ‘ + data);
});
myEmitter.emit(‘ping’, ‘My first Node.js event has been triggered.’);
What’s the difference of doing:
function ping(data){
console.log(“First Event: ” + data)
}
ping(‘My first Node.js event has been triggered.’)
The difference in the two scenarios you listed is when you use Events your functions are fired in response to an event while simply calling a function means the functions are fired almost immediately.
The difference is that anytime that event is triggered asynchronously, the event handler prints out the data sent to it. The event handler can do anything like send new signup email or subscription reminder emails. The event can be triggered multiple times as long as the app is running.