0

I am executing the below code.

var sum = 0;

var calculateSum = function(input) {
    for(var i=0;i<input;i++) {
        sum+=i;
    }
    console.log("Calculated sum for " + input);
    return sum;
}

function calculateValue(data, callback) {
    callback(data);
}

calculateValue(5000000,calculateSum);
while(sum<5) {
    console.log("Still calculating sum!!!");
}

console.log("Calling third");

I expect the output to be

Calling third

Still calculating sum

Calculated sum for 5000000

But, the output is

Calculated sum for 5000000

Calling third

It is the same as synchronous programming. Please tell me what I am doing wrong.

Thanks,

Divya

4
  • 1
    Protip: it's not the same as synchronous programming. Commented Oct 6, 2016 at 15:43
  • If you're attempting asynchronous programming, give this a read - bytearcher.com/articles/… Commented Oct 6, 2016 at 15:45
  • What exactly are you trying to do? Commented Oct 6, 2016 at 15:48
  • Hi mtsdev, I am new to Nodejs and am just trying out an example. From what I understand, Nodejs executes calculateValue(), while() and console.log() in parallel. So I expect console.log() and while() to finish first and give me the output before calculateValue(). Commented Oct 6, 2016 at 16:14

5 Answers 5

1

Using callbacks does not make a code asynchronous, and you must understand that Javascript is singled-threaded (most of the times), so what your code does is:

  1. Calls calculateValue
  2. calculateValue calls calculateSum
  3. calculateSum is executed and its payload is shown
  4. Then the while condition is checked. It results be false, so it's block is not executed
  5. The final line is executed, being that message added to the console.
Sign up to request clarification or add additional context in comments.

Comments

0

You call calculateValue(5000000,calculateSum); first. The only console.log() in there is console.log("Calculated sum for " + input);. So, this will be the only thing logged when calling this method, which also changes your sum to > 5. You then compare sum < 5, but sum is already > 5 because of your previous function call. The very last thing you call is console.log("Calling third");, so one would expect that to be the last thing to happen.

Comments

0

the output seems correct. It calls calculatedValue() first which prints "Calculated sum for 5000000" and since sum is > 5 so the while loop is skipped. Then log the final message. If you want parallel programming or delayed one. You can use Timer.

Comments

0

Everything in your code is synchronous. Javascript executes the code line by line. That is the reason script execution will wait for the calculateSum function to complete execution , print the log statement inside it and then move to the next line. As the (sum <5) condition is false , it is directly printing the last console.log statement. This is how your script runs :

  1. Initialize sum = 0.
  2. call the function calculateValue(5000000, calculateSum);
  3. call the calculateSum function from calculateValue function.
  4. Inside calculateSum function , loop through the given input from 0 and log the first console.log statement i.e

    console.log("Calculated sum for " + input); 5.Evaluate (sum<5) which returns false as the sum value is has already changed, so skip the while loop.

  5. log the final console.log statement i.e

    console.log("Calling third");

Comments

0

Thankyou all for the responses.

I think I may have understood something fundamentally wrong about Node JS. Kindly correct me if my understanding given below is wrong.

calculateValue(), while() and console.log() are treated as three separate executions.

So, whichever finishes first is written to console.

Since calculateValue() takes sometime to finish, while() and console.log() completes execution first and writes to the console.

I have created calculateValue() as a replacement to readFromDB(). So, the flow should be

readFromDB(), while() and console.log() are treated as three separate executions.

So, whichever finishes first is written to console.

Since readFromDB() takes sometime to finish, while() and console.log() completes execution first and writes to the console.

Is this not supposed to be the main advantage of Node JS. i.e. if reading from DB takes a long time, the system can proceed with other actions like accepting user input/performing unrelated calculations.

Thanks, Divya

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.