You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+37-25Lines changed: 37 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -840,44 +840,56 @@ Sum: 30
840
840
841
841
## Q. What are the difference between Events and Callbacks?
842
842
843
-
**1. Callbacks:**
843
+
**1. Events:**
844
844
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.
846
846
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:**
848
850
849
851
```js
850
-
functiongreeting(name) {
851
-
alert('Hello '+ name);
852
-
}
852
+
/**
853
+
* Events Module
854
+
*/
855
+
constevent=require('events');
856
+
consteventEmitter=newevent.EventEmitter();
857
+
858
+
// add listener function for Sum event
859
+
eventEmitter.on('Sum', function(num1, num2) {
860
+
console.log('Total: '+ (num1 + num2));
861
+
});
853
862
854
-
functionprocessUserInput(callback) {
855
-
var name =prompt('Please enter your name: ');
856
-
callback(name);
857
-
}
863
+
// call event
864
+
eventEmitter.emit('Sum', 10, 20);
858
865
859
-
processUserInput(greeting);
866
+
// Output
867
+
Total:30
860
868
```
861
869
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:**
865
871
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.
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