What's callback?
- Passing a function definition to another function as an argument is callback.
- 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. ~MDN
function callbackFirst(arg1,arg2,arg3){
console.log(arg1(), arg2(), arg3() );
};
callbackFirst( function callbackSecond(){
return "Returned second callback";
} , function callbackThird(){
return "Returned third callback";
} , function callbackFourth(){
return "Returned fourth callback";
} );
The above function will return "Returned second callback Returned third callback Returned fourth callback" as output.
How to differentiate between synchronous and asynchronous callback?
Only setTimeout, setInterval, requests and events are asynchronous, while the other built-in callbacks are synchronous like Array.prototype.map