51. Explain Callback in Node.js
Callbacks are a normal function which is passed as a parameter to any asynchronous functions operation's completion.
It is invoked inside the another functions in which its been passed.
It's just a convention and an asynchronous equivalent for a function.
Node.js works on callbacks chain to maintain synchronization of asynchronous operations.
Due to heavy use of callbacks, there may also have callback Hell in Node.js.
Callbacks are used to write Non-Blocking Codes.
52. What is Callback hell in Node.js ?
Node.js works on the single thread non-blocking event loop, which passes another events as a callback next to be fired after any event completion.
Example of Callback Hell:
This type of calling callbacks in any event loop, causes callback hell in Node.js
This chain could be so long until the event loop ends, when there is no next callback event.
53. How to avoid Callback hell in Noide.js ?
There are multiple techniques for dealing with callback hell.
By using Async.js
By using Promise
By using Async/Await
54. What's the first argument passed to a Node Js callback handler ?
error is passed as a first argument to any callback handler function.
This will execute the function, rather than returning the function.
This will return the function and will not execute the function.
56. What is an error-first callback ?
All the callbacks have the first argument as the error returned form the parent function.
So that the error can be handled at the very first step in the callback, if any error is thrown in the previous function.
57. How Promises are better than callbacks?
promises are just wrapper on callbacks.