Skip to content

Commit 7655c93

Browse files
committed
Update README.md
1 parent 2b57187 commit 7655c93

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

README.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -840,44 +840,56 @@ Sum: 30
840840

841841
## Q. What are the difference between Events and Callbacks?
842842

843-
**1. Callbacks:**
843+
**1. Events:**
844844

845-
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
845+
Node.js **events** module which emits named events that can cause corresponding functions or callbacks to be called. Functions ( Callbacks ) listen or subscribe to a particular event to occur and when that event triggers, all the callbacks subscribed to that event are fired one by one in order to which they were registered.
846846

847-
**Example:** synchronous callback
847+
All objects that emit events are instances of the **EventEmitter** class. The event can be emitted or listen to an event with the help of EventEmitter
848+
849+
**Example:**
848850

849851
```js
850-
function greeting(name) {
851-
alert('Hello ' + name);
852-
}
852+
/**
853+
* Events Module
854+
*/
855+
const event = require('events');
856+
const eventEmitter = new event.EventEmitter();
857+
858+
// add listener function for Sum event
859+
eventEmitter.on('Sum', function(num1, num2) {
860+
console.log('Total: ' + (num1 + num2));
861+
});
853862

854-
function processUserInput(callback) {
855-
var name = prompt('Please enter your name: ');
856-
callback(name);
857-
}
863+
// call event
864+
eventEmitter.emit('Sum', 10, 20);
858865

859-
processUserInput(greeting);
866+
// Output
867+
Total: 30
860868
```
861869

862-
**2. Events:**
863-
864-
Node.js **events** module which emits named events that can cause corresponding functions or callbacks to be called. Functions ( Callbacks ) listen or subscribe to a particular event to occur and when that event triggers, all the callbacks subscribed to that event are fired one by one in order to which they were registered.
870+
**2. Callbacks:**
865871

866-
All objects that emit events are instances of the **EventEmitter** class. The event can be emitted or listen to an event with the help of EventEmitter
872+
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
867873

868-
**Example:**
874+
**Example:** synchronous callback
869875

870876
```js
871-
var event = require('events');
872-
var eventEmitter = new event.EventEmitter();
873-
874-
// Add listener function for Sum event
875-
eventEmitter.on('Sum', function(num1, num2) {
876-
console.log('Total: ' + (Number(num1) + Number(num2)));
877-
});
877+
/**
878+
* Callbacks
879+
*/
880+
function sum(number) {
881+
console.log('Total: ' + number);
882+
}
878883

879-
// Call Event.
880-
eventEmitter.emit('Sum', '10', '20');
884+
function calculator(num1, num2, callback) {
885+
let total = num1 + num2;
886+
callback(total);
887+
}
888+
889+
calculator(10, 20, sum);
890+
891+
// Output
892+
Total: 30
881893
```
882894

883895
Callback functions are called when an asynchronous function returns its result, whereas event handling works on the **observer pattern**. The functions that listen to events act as Observers. Whenever an event gets fired, its listener function starts executing. Node.js has multiple in-built events available through events module and EventEmitter class which are used to bind events and event-listeners

0 commit comments

Comments
 (0)