Skip to main content

Command Palette

Search for a command to run...

Callback

Published
1 min readView as Markdown

Callback is a function in which another function is passed as an argument and it is called after certain work is completed in the parent function. Since JavaScript is a single threaded language this callback enables the JavaScript to be asynchronous.

Example:

Explanation :

In the above example we have two functions sum and operation. The parent function is the operation and the function sum which is passed as an argument is the child. The child function is called inside the parent function at a point after certain console.log operations were done. This is called as callback function.

Example 2:

Explanation:

In the above example we have two functions one is fetchData and another is displayData. The displayData is passed as an argument to the fetchData function and the setTimeout timer is triggered once the fetchData function is called and after 2 seconds the callback function is pushed into the callback queue. The event loop assign it into the call stack and it gets executed.