Evan Harmon - Memex

JavaScript Asynchronous

  • Concepts Related To: Blocking, non-blocking, concurrency, threading, parallel, asynchronous, async/await etc.
  • Non-sequential, async, etc.
  • JavaScript is an event-driven programming language.
  • When a JavaScript application is launched, all the code in that application is loaded into memory. Every variable, function, and block of code is made available to the application, whether or not the code is executed right away. Why might certain code not run right away? Although defining and assigning a global variable may give that variable a value as soon as the application is launched, not all functions run unless they have a reason to do so. Some of these functions come in the form of event listeners—function objects that run a corresponding callback function when an event with a matching name is emitted. These functions sit around in memory until event emitters—objects that fire event names—trigger the event listeners to run their callback functions.
  • On most other platforms, incoming tasks are assigned to new processes, creating a new event loop for each task. Increasing the number of tasks, however, is like increasing the number of employees in a finite space. You start to run into new issues such as cost, computing power, and shared resources. (What would you do if two employees need to use the phone at the same time, for example?)
  • A process is also a bundle of computing power and resources used for a task’s execution, though usually for larger tasks than those handled by threads. A process must exist to create a thread, which implies that each Node.js application runs on its own process.
  • Node runs on a single thread
  • Contra java e.g. where each request creates different thread
  • Downside to this is that each thread might take 2MB
  • You have to really make sure the main/single node thread isn’t hung up on something because it doesn’t have another thread to be listening for another request e.g. Why you need all these callbacks.
  • Asynchronous so you don’t need multi-threading
  • Asynchronous means Not simultaneous or concurrent in time.
  • Vs Synchronous which means Simultaneous or concurrent in time.
  • When fetching data in JavaScript, the HTTP requests are asynchronous. In brief, that means when an HTTP request is sent, we don’t know exactly when a response will be received by the browser.
  • There are synchronous ways of doing things if you don’t want the async, but you normally want to do it async like normal

Events

Event Loop

Node Queue

JavaScript Asynchronous
Interactive graph
On this page
JavaScript Asynchronous
Ajax (programming)
JavaScript > Async/Await
DOM (Document Object Model) > Events
Events
Event Loop
JavaScript Callback Functions
Node.js
Node Queue