2

I have read that

Node.js is Single Threaded, i.e. it executes the code in a single sequence or direction. At a given time, only a single task/ call is executed. Asynchronous and Single-Threaded: Execution doesn’t wait for the current request to complete and moves to the next request/call.

I am confused between the two Single-Threaded and Asynchronous. How is then process handled like if I perform some sequence of operations when one task is setTimeout, another is fetching data from API, other is some arithmetic operations? Then what would be the sequence of operations considering its single threaded?

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
Pranshu Kashyap
  • 115
  • 1
  • 7
  • 4
    research the javascript event loop. – Daniel A. White Dec 05 '21 at 19:23
  • 2
    Asynchronious is basically a `delayed execution`.. – Silvan Bregy Dec 05 '21 at 19:24
  • 1
    When YOU do a single thread, like folding your clothes... You find on piece of clothe that isn't washed correctly and you throw it aside to process later. – Louys Patrice Bessette Dec 05 '21 at 19:37
  • 4
    @SilvanBregy - delayed execution is not an appropriate description at all of asynchronous operations. When you do `fs.read()`, there's no delayed execution at all. The operation is initiated immediately and then the function returns immediately. The operation proceeds in the background and some time later, you get notified via your callback function when it has completed. There's nothing "delayed" about this execution. `setTimeout()` is a means of scheduling some code to run at some point in the future so that's the only place you might have delayed execution. – jfriend00 Dec 05 '21 at 19:41
  • 1
    @SilvanBregy jfriend00 is right. It's more of a delayed *resolution*, so an action can happen *now* but have its final result *later*. – VLAZ Dec 05 '21 at 19:47
  • Thanks for correcting. My plan was to give a one liner and that's it . Maybe you guys are interested in reading about `Cunningham's Law` ;) – Silvan Bregy Dec 06 '21 at 08:19

1 Answers1

6

"Single threaded" in this case means that it runs YOUR Javascript in a single thread. That means that it runs a piece of your Javascript until it returns control back to the system and only then can it run the code attached to some other event. No two pieces of your Javascript are ever actually executing at the same time.

FYI, we're ignoring WorkerThreads here for the purposes of this discussion which are a purposeful way to run multiple threads of Javascript, but are not involved in the general asynchronous architecture.

Asynchronous operations are ALL implemented in native code (usually C/C++ code) by nodejs. They are things such as timers, networking operations, disk operations, etc... That native code (mostly in a cross platform library called libuv) has interfaces in Javascript that allows you to call them such as http.request() or fs.read(), but the underlying core implementation of those functions is in native code. The native code for those operations may or may not actually use OS threads in its implementation. But, those threads are entirely hidden from the Javascript code.

For example, networking operations in nodejs do not use threads. They use natively asynchronous APIs in the operating system that allow them to be notified when a network operation has completed without blocking or spinning and waiting for it. Other asynchronous operations such as file operations do actually use a pool of native OS threads to "simulate" an asynchronous architecture.

So, while your Javascript is run as single threaded, multiple asynchronous operations can be in process at once. If you look at this code:

const fsp = require('fs').promises;

Promise.all([fsp.readFile("filea.txt"), fsp.readFile("fileb.txt")]).then(results => {
   console.log(results[0]);
   console.log(results[1]);
}).catch(err => {
   console.log(err);
});

This will read two files in parallel and tell you when the data from both files is available. While your Javascript still runs as single threaded, the underlying file operations are using OS-level threads and the disk operations are running in separate threads. So, it's only your actual Javascript that is single threaded, not necessarily the underlying asynchronous operations.

Fortunately, you can generally avoid entirely the concurrency headaches of multi-threaded programming because two pieces of your own Javascript are never running at the same moment in time. Thus, you don't need mutexes or other concurrency devices just to write to variables safely. The details of thread use are abstracted behind the asynchronous interfaces. And, the event driven nature of how nodejs handles the completion of these asynchronous interfaces dictates that one piece of Javascript will run to complete before the next completion event can be processed that has the next asynchronous result in it.

jfriend00
  • 683,504
  • 96
  • 985
  • 979