Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Asynchronous Vs Synchronous programming

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 147
    Comment on it

    In this tutorial we will come to know about the difference between the working of synchronous and asynchronous programming. To explain the difference let us take two examples, one for asynchronous and another for synchronous programming.

    First example for synchronous programming:

    var output = database.query("Select * from employee");
    console.log("output: 1");
    console.log("output: 2");

    Second example for asynchronous programming:

    database.query("Select * from employee", function(output) {
        console.log("output: 1");
    });
    console.log("output: 2");

    The difference in both way of programming:

    In synchronous programming, each step is executed once the previous one is finished. In asynchronous programming we us a callBack function, which will run as soon as we get the out from database in the mean while the program executes other task. We prefer to go for Asynchronous programming If operations in our application are doing very heavy lifting like querying large data from the DB else it's okay to go with Synchronous programming.

    In first example the program will be blocked in the first line until it receives the output and hence the output will be as below:

    output: 1

    output: 2

    In second example the program will not be blocked in the first line rather it will execute the output as below:

    output: 2

    output: 1

    In the second example of asynchronous programming, the console.log("output: 2"); is executed while the query is being processed in the background and the program will do some other tasks.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: