Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback function example

I am having a hard time understanding how the callback() function is used in the following code block.

How are we using callback() as a function, in the function body, when function callback() has not been defined?

What are the repercussions of passing true / false as parameters into the callback function below?

I appreciate any clarification, thanks in advance!

socket.on('new user', function(data, callback){     if (nicknames.indexOf(data) != -1){         callback(false);     } else{         callback(true);         socket.nickname = data;         nicknames.push(socket.nickname);         updateUserList();     } }); 
like image 273
AnchovyLegend Avatar asked Mar 16 '14 20:03

AnchovyLegend


People also ask

What are callback functions with an example?

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. The above example is a synchronous callback, as it is executed immediately.

How do you write a callback function?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.

What is meant by callback function?

A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function, which contains the logic for when the callback function gets executed.

Why callback function is used?

Need of Callback Functions. We need callback functions because many JavaScript actions are asynchronous, which means they don't really stop the program (or a function) from running until they're completed, as you're probably used to. Instead, it will execute in the background while the rest of the code runs.


1 Answers

When you pass a function as an argument, it is known as a callback function, and when you return a value through this callback function, the value is a parameter of the passed function.

function myFunction(val, callback){     if(val == 1){         callback(true);     }else{         callback(false);     } }  myFunction(0,  //the true or false are passed from callback()  //is getting here as bool // the anonymous function below defines the functionality of the callback function (bool){     if(bool){         alert("do stuff for when value is true");     }else {         //this condition is satisfied as 0 passed         alert("do stuff for when value is false");     } }); 

Basically, callbacks() are used for asynchronous concepts. It is invoked on a particular event.

myFunction is also callback function. For example, it occurs on a click event.

document.body.addEventListener('click', myFunction); 

It means, first assign the action to other function, and don't think about this. The action will be performed when the condition is met.

like image 188
Suman Bogati Avatar answered Oct 14 '22 19:10

Suman Bogati