You Don’t Know JS

The goals of asynchronous JavaScript

From the earliest days of the web, JavaScript has been a foundational technology that drives interactive experience around the content we consume. During Programmer’s Week 2019, Cognizant Softvision’s largest technical event, Sandeep Mohan and Benson Zachariah explained some of the lesser understood concepts of JavaScript.

The general conception of JS is that it is a deeply flawed language that was poorly designed and inconsistently implemented. Many have asserted that it’s the “worst most popular language” in the world; that nobody writes JS because they want to, only because they have to, given its place at the center of the web. But we aim to take a deep dive into a few concepts in order to appreciate this language.

Javascript is always synchronous and single-threaded. However, to achieve asynchronicity, three actions need to be taken:

  1. Initiate a task that takes a long time (e.g. requesting data from the server).
  2. Move on to more synchronous regular code in the meantime
  3. Run some functionality once the requested data has come back

To achieve these goals we can use certain JS features:

  • Promises: Promises provide a trustable intermediary — that is, between your calling code and the async code that will perform the task — to manage callbacks.

Yet another way of conceptualizing a Promise is that it’s a future value, a time-independent container wrapped around a value. This container can be reasoned about identically whether the underlying value is final or not. Observing the resolution of a Promise extracts this value once available. In other words, a Promise is said to be the async version of a sync function’s return value.

A Promise can only have one of two possible resolution outcomes: fulfilled or rejected, with an optional single value. If a Promise is fulfilled, the final value is called a fulfillment. If it’s rejected, the final value is called a reason (as in, a “reason for rejection”). Promises can only be resolved (fulfillment or rejection) once. Any further attempts to fulfill or reject are simply ignored. Thus, once a Promise is resolved, it’s an immutable value that cannot be changed.

  • Closures: When an inner function is defined, immediately a bond will be created with the surrounding data, this bond with the “Live persistent data” doesn’t break when the function is returned out (When it is popped off the call stack and it’s execution context gets deleted).​This live data gets attached to the underlying function like a “Backpack” to the function definition itself.   

​The data has been “Closed Over” and returned out in the variable environment, hence it is called a “Closed over variable environment” (COVE), also commonly referred to as “Closures.”​

As learned in the Javascript Programmers’ Week discussion, Javascript is always synchronous and single-threaded, however some features can be used to introduce asynchronicity.

Programmers’ Week 2019 was a great opportunity to listen to renowned speakers discussing cutting edge projects, emerging technologies, innovation case studies, and more. This year, Programmers’ Week takes place September 14-18 and includes over 40 different tech talks and discussions from global thought leaders from a variety of industries and tech expertise. To learn more, click here.

 

Article Sources: 

  1. The New Hard Prats from Frontend Masters: https://frontendmasters.com/courses/javascript-new-hard-parts/​
  2. The hidden Power of JavaScript Generator and Promises​: https://hackernoon.com/using-javascript-generator-and-promises-77d7dc977​
Background Image