0

I just wanted to understand concept of nodejs and javascript . My question is Nodejs framework claims that it provides asynchronous way to handle jabvascript so this asyncronous way of handling javascript code is introduced by nodejs or it is already availaible in js ?

And also nodejs only runs js code in asyncronous way ?

please help me to understand this.

Thanks in advance.

Kalana Demel
  • 3,220
  • 3
  • 21
  • 34

1 Answers1

0

Asynchronicity is nothing introduced or invented by nodejs, it existed long before in browsers (e.g. setTimeout, XMLHttpRequest, addEventListener) , but is is heavily used by nodejs.

Asynchronicity has nothing to do with parallelism and multithreading at first, but says something about when an action you initiate is performed and when you get its result back.

So if you have a code like this:

 doSomeSyncActionA();
 doSomeAsyncActionB();
 doSomeSyncActionC();

Then action B will initiated at the time doSomeAsyncActionB is called but is not expected to be finished at the time doSomeSyncActionC is called.

Why/how that asynchronicity happens in doSomeAsyncActionB is not relevant.

And as doSomeAsyncActionB does some async stuff you cannot return data directly form it. You either have to wait for the data using Promises and await or use callbacks.

 doSomeSyncActionA();
 await doSomeAsyncActionB();
 doSomeSyncActionC();

Now doSomeSyncActionC is called after the result of doSomeAsyncActionB is finished because of the await, but that does not mean that the shown code block is now sync. Another important part is that code is sync if no other code (of the same context) can be executed in between.

t.niese
  • 39,256
  • 9
  • 74
  • 101