+ 2

What are callback in JS and why do we need them?

Can anyone explain callbacks with an example plz

3rd Mar 2019, 12:44 AM
HAMDI
HAMDI - avatar
4 Réponses
+ 9
L'artisto hamdi Maybe you are doing something wrong. Try rechecking syntax or u can post ur code so that we can help u out.
10th Mar 2019, 10:52 AM
Arushi Singhania
Arushi Singhania - avatar
+ 8
We need callbacks in js as if u have called a function and if it took some time to execute and then your code will move ahead and will not wait for the full execution of your first function. Now, if your second function need some data from ur first function, it will arise an error. Hence, we use callbacks so that a function is called only after the another function has completed its execution.
3rd Mar 2019, 5:27 AM
Arushi Singhania
Arushi Singhania - avatar
+ 3
Callbacks are functions which need to be called after a certain work is done and may be with arguments as result of the previous work. Suppose, you're trying to load some data from the server, may be some Json data, then you'll write a function which will be called once the data is loaded from the server. This function is called a callback function. Ex. $.load(url){ //some other methods/properties, callBackFunc(result){ console.log(result); } , //may be some other methods/properties }
3rd Mar 2019, 1:56 AM
Шащи Ранжан
Шащи Ранжан - avatar
0
The probleme is even when i use a callback javascript still moves on not waiting for the response of the previous function !
4th Mar 2019, 6:41 PM
HAMDI
HAMDI - avatar