Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Events and Functions?

I am new to Node, and I am struggling to understand the main difference between Events and Functions. Both need to be triggered, so why do we need an Event at all if we have to trigger it anyway?

How is it different than having a Function triggered?

Example code:

var events = require('events');
var eventEmitter = new events.EventEmitter();

eventEmitter.on('event1', function () {
    console.log('Event 1 executed.');
    eventEmitter.emit('event2');
});

eventEmitter.on('event2', function() {
    console.log('Event 2 executed.');
});

eventEmitter.emit('event1');
console.log('Program Ended.');

We can achieve the same result by functions, right?

I am sure this has some serious importance in Node (otherwise it would not exist, lol), but I am struggling to understand it.

Help appreciated! :)

like image 604
Ariel Weinberger Avatar asked Feb 14 '16 18:02

Ariel Weinberger


People also ask

What is the difference between events and Functions unreal?

Events cannot have return values or local variables like the Functions but the key difference is that they are asynchronous by nature, this means a couple of things: You can manipulate time inside events (e.g., delays or timelines) You can replicate them over network, essential for any multiplayer game.

What is the difference between event and function in solidity?

What is the difference between events and functions in Solidity? While both functions and events accept arguments and can be called, functions modify smart contracts directly while events have the role of informing services outside of the blockchain to let users know that something has happened.

What is the meaning of function event?

An event function definition includes a general description of the function, the location and schedule, the registration fee information including account and fundraising data, and optional room setup and planning details.

What is the difference between event and process?

The difference between events and processes is simple. Event: the idea that results could be traced back to one thing happening. Process: the idea that results occurred because of cumulative, long-term efforts.


1 Answers

Events deal with asynchronous operations. They aren't really related to functions in the sense that they are interchangeable.

eventEmitter.on is itself a function, it takes two arguments the event name, then a function (callback) to be executed when the event happens.

eventEmitter.on(evt, callback)

There is no way to tell WHEN the event will be emitted, so you provide a callback to be executed when the event occurs.

In your examples, you are controlling when the events are triggered, which is different than real world use where you may have a server listening for connections that could connect at anytime.

server.listen('9000', function(){
    console.log('Server started');
});

server.on('connection', function(client){
    console.log('New client connected');
    doSomethingWithClient(client);
});

//series of synchronous events
function doSomethingWithClient(client){
    //something with client
}

For server.listen the server doesn't start immediately, once its ready the callback is called

server.on('connection') listens for client connections, they can come at any time. The event is then triggered when a connection occurs, causing the callback to be run.

Then there is doSomethingWithClient this is just a function with a set of synchronous operations to be done when a client connection occurs.

like image 76
Gabs00 Avatar answered Sep 28 '22 19:09

Gabs00