I'm struggling with understanding how Javascript can maintain a single-threaded nature while still being non-blocking on the client. My current mental model is akin to an assembly line:
Imagine a single assembly line at the beginning of code execution, piecing together various components to build a car.
As we reach 20% completion, it's time to add the engine, but the engine isn't ready yet.
Instead of halting operations for the engine to be completed, the assembly line splits into two - is it two threads?
One section carries on assembling other parts of the car (Line 1).
The second part focuses on constructing the engine (Line 2).
After the engine assembly finishes, Line 2 merges back into Line 1 and adds the engine.
At this point, Line 1 could be anywhere from 30% to 99% complete depending on the speed of the engine assembly.
This analogy illustrates my perception of non-blocking, asynchronous coding. The primary thread in Line 1 can progress without waiting for Line 2 to finish. However, this hypothetical assembly line setup needs dual lines or threads, which contradicts the single-threaded nature of JS.
These contradictions have left me in a state of confusion.