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.
- Initiate a task that takes a long time (e.g. requesting data from the server).
- Move on to more synchronous regular code in the meantime
- 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.”
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.