Synchronous
programming means that, barring conditionals and function calls, code
is executed sequentially from top-to-bottom, blocking on long-running
tasks such as network requests and disk I/O.
Asynchronous
programming means that the engine runs in an event loop. When a
blocking operation is needed, the request is started, and the code keeps
running without blocking for the result. When the response is ready, an
interrupt is fired, which causes an event handler to be run, where the
control flow continues. In this way, a single program thread can handle
many concurrent operations.
User
interfaces are asynchronous by nature, and spend most of their time
waiting for user input to interrupt the event loop and trigger event
handlers.
Node
is asynchronous by default, meaning that the server works in much the
same way, waiting in a loop for a network request, and accepting more
incoming requests while the first one is being handled.
This
is important in JavaScript, because it is a very natural fit for user
interface code, and very beneficial to performance on the server.
Comments
Post a Comment