I am reading various tutorials on javascript with callbacks, but they are not clarifying a basic principle. What I understand is that when a function is called with a callback, this makes it possible to wait on execution of some code, and after that execute the callback function. So
// function definition
do_func(int num, callback)
{
Console.log(num);
..
..
callback();
}
//call the function
do_func(123, function(){
Console.log("Running callback");
});
So during execution of do_func, all lines of code are executed, and then callback() is executed. However if our function changed as
// function definition
do_func(int num, callback)
{
read file...
..
..
callback();
}
Then the callback would be called while the file is being read. So our purpose is defeated. What is happening here?
Thanks very much!
synchronous Vs Asynchronousread file? How are you reading it? If it's asynchronous and takes a callback, you would have to pass your callback to it (possibly wrapped in an anonymous function).