This post is a continuation of "Javascript 105: Callbacks". So If you have not studied the last post please go back and then come back to this. As discussed in the last post, a callback is a way to force synchronous (sequential) operation of otherwise asynchronous tasks. Since this is a continuation lets begin from where we left off with the last post. Take a look at this code, and its output. Output: We did that to ensure the statements prints in order This is okay for situation like this. But imagine a code as follows Output: Take a look at the code and the output carefully. It is obvious that these statements are printing out of order due to the inherent asynchronous nature of JS. If we still want to ensure that they take their own time to execute but still executing the way we want, how do you think that we can make this happen. Sure, we can create multiple callback functions and call them inside of each (Which I even do not w
Javascript is asynchronous. That means you cant guarantee that the code you write will always execute sequentially. Take the following code for example, Output When looking at the output, it is understandable that the statements has been executing sequentially. But if I do a modification to the code like the following, the output will be different. Output: Don't worry about the line number 6 yet. the amendment there will just delay the the execution of the statement "console.log('statement 01')" by 100 milliseconds. Now the "statement 01" prints out at the end of the line. The change of the output can be explained as follows. the JS interpreter sees the line 6 (which is the first line of the script), and realize that it has to wait for a 100 milliseconds before executing the statement "console.log('statement 01')", but sees that other four lines has no such delay and can be executed right away. So it goes a