A callback function is a function that’s called by another function. It can be used to set up an event or action in the future, such as when a user clicks on something on your website. In programming terms, it’s just like setting up an alarm clock!
Call back functions can be used when your event or action needs to happen in the future. It’s just like setting up an alarm clock – it will call at specific times and do things for us so that we don’t have to worry about it happening ourselves (a type of convenience feature). When the function completes, it executes a callback to signal its completion.
Asynchronous programming is a very powerful technique, which allows for multiple actions to happen simultaneously. Useful in situations where the main program loop can’t wait on individual functions before continuing with other tasks. A callback function simply fulfills a certain task that has been requested by another part of your code and then returns control back so you don’t need to worry about it again!
In a nutshell:
- A callback allows another function to access certain functions.
- A callback is invoked after the first function completes.
Examples
In JavaScript, we use a callback in setTimeout() function to execute it after a defined period.
setTimeout(function(){ alert("I'm called after 3 seconds"); }, 3000);