Tuesday, 16 April 2019

Difference between Callbacks and events in NodeJs



The topics "Asynchronous Notifications Using Call-Back Methods" and "Asynchronous Notifications Using Events" demonstrate that call-backs are more work to implement than events. However, you should not base your decision about which to use on the amount of work involved. Call-backs and events represent different approaches to communication, and you should select the approach that best fits your needs.
Events Call backs
An event is like an anonymous broadcast A call-back is like a handshake.
The component that raises events knows nothing about its clients The component that makes call-backs knows a great deal
A client that has a reference to an object that raises events can handle those events . The object that raises the events has no information about its clients. It's broadcasting to an unknown audience — perhaps to an empty theater. By contrast, a component that's making call-backs must have a reference to every object it's going to call. It has to know exactly how many of them there are.
An object that raises events has no control over the order in which clients receive its events. (And you should take care to avoid creating dependencies on any ordering you may observe.) A component making call-backs can control the order in which it calls clients back. It might give some clients higher priority, for example.
When an object raises an event, all of its clients handle the event before the object that raised the event gets control again. a component making call-backs gets control back after each call it makes to a client.
If an event contains a ByRef argument, that argument can be altered by any client that handles the event. The last client's changes are the only ones visible to the object that raised the event, because (as noted above) the object that raised the event doesn't get control again until all of its clients have handled the event. a component making call-backs can examine changes to ByRef arguments after every call to a client, and can pass the next client fresh values for those arguments.
If an unhandled error occurs in a client's event handler, the object that raised the event cannot receive the error. If the object is provided by an in-process component running in the client’s address space, both client and component will terminate as a result of the unhandled error. a component making call-backs will receive errors that occur in the call-back method, and must be prepared to handle them.
Events cannot have optional arguments, named arguments, or ParamArray arguments. Callbacks can have all of these arguments
Example  - Think of a Server (Employee) and Client (Boss).One Employee can have many Bosses. The Employee Raises the event, when he finishes the task, and the Bosses may decide to listen to the Employee event or not.The employee is the publisher and the bosses are subscriber. Example - The Boss specifically asked the employee to do a task and at the end of task done, the Boss wants to be notified. The employee will make sure that when the task is done, he notifies only the Boss that requested, not necessary all the Bosses. The employee will not notify the Boss, if the partial job is done. It will be only after all the task is done.Only one boss requested the info, and employee only posted the reply to one boss.

No comments:

Post a Comment