1

I was recently tinkering with node js and its asynchronous nature and came across something weird. Here is the code -

let sum = 0;
for(let i=0;i<10000000000;i++){
    sum += i
    if(i==99999999){
        console.log(sum);
    }
}

console.log('abc');

By definition of non blocking,it should not wait for the for loop results and should print 'abc' first and then print the value of sum after completion of calculation right? However,this is not happening and the program is waiting for the for loop to finish and print the value of sum and then print 'abc'. Could anyone explain the reasoning behind this? Is this happening due to the way console.log works?

  • 3
    A `for` loop isn't asynchronous, it's blocking until finished. – Teemu Oct 06 '20 at 06:31
  • Didn't know that. Thankyou! – Sushmit Chakraborty Oct 06 '20 at 06:38
  • You can perform asynchronous tasks with JS, but the language itself is not asynchronous. The actual tasks are executed in the environmet (ex. Node, browser, Web API ...) where JavaScript is run, and that environment might well be multi-threaded. When you set a handler for an asynchronous task, that handler is not the task itself, it's synchoronous JavaScript (no other functions can be executed concurrently) just handling the result of the task. – Teemu Oct 06 '20 at 06:48
  • There is no async code here. A clue to async code in JS, if it uses a callback or returns a Promise. Although even then it's not 100% the case, eg. The callbacks in Array.forEach etc are still synchronise. – Keith Oct 06 '20 at 06:55

2 Answers2

2

Its non blocking for IO operations, IO operations include file read write, network read write, database read write etc.

This is controlled by event loop.

For loop is not IO operation, however if you have IO operation inside for loop you will get the non blocking taste.

You can read more here https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/

r7r
  • 1,440
  • 1
  • 11
  • 19
  • Ah I see! Thankyou for the explanation. One question though,why is javascript mentioned as a single threaded asynchronous language if it is not asynchronous only for some specific cases? – Sushmit Chakraborty Oct 06 '20 at 06:37
  • the node runtime is single threaded, but it uses c++ libuv libraries to achieve asynchronous behavior which is multi threaded, this is controlled by event loop, you can read more here https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ – r7r Oct 06 '20 at 06:41
0

Javascript works synchronouse. There are few things that work asynchronouse:

setImmediate, setTimeout(), and promises like fetch(), Promise.resolve() etc.

I also need to mention that the callback from the setTimeout for example is getting added to an task que or also named "callback que" and its waiting till the event loop is done with its synchronouse code. After the event loop is ready it will take this callback and add it to the callstack and execute that callback function. And this code will be executed synchronouse.

There is actually no true parallelism. For that you should use web workers.

bill.gates
  • 14,145
  • 3
  • 19
  • 47