loading...
Cover image for JavaScript Interview Questions

JavaScript Interview Questions

lassiecoder profile image lassieCoder ・1 min read

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

Discussion

pic
Editor guide
 

A callback is when you meet someone nice at the party and offer your phone number with the instructions "call me :)" and then hope you get a call later.

Synchronous callback is when you sit there staring at your phone waiting for that call.

Asynchronous callback is when you're busy doing your own thing and forgot you gave that number in the first place.

 

Love teh way you explained it 👏🏻😃

 

Thanks, I've been trying to improve my #ELI5