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.