JavaScript - The Future Is Now - Mastering JavaScript Promises (2015)

Mastering JavaScript Promises (2015)

Chapter 9. JavaScript - The Future Is Now

So far in the previous chapters, we focused on how to become indispensably good at applying the concept of promises in different JavaScript libraries and how we can get maximum advantages in our future projects. However, this is not all about JavaScript.

Although promises are huge and their implementations can bring a number of benefits, that's not the end of JavaScript. In fact, JavaScript has more to offer in the coming years than we can have even thought of. It is the progressive language of the modern age and it's gaining popularity day by day. What else can JavaScript offer us? We will try to find out in this chapter.

Let's start with ECMAScript 6.

ECMAScript 6 (ECMA 262)

ECMAScript Language Specification is in its sixth version. Since the time its first version was published in 1997, ECMAScript has become one of the world's most vastly adopted general purpose programming languages. It is known for its ability to embed itself in web browsers along with its ability to use server-side and embedded applications.

Many consider the sixth edition to be the most detailed and most widely covered update of ECMAScript since its inception in 1997.

We will consider the sixth edition of ECMA 262 for our discussion in this chapter; it's a draft version with the aims to include better support for large applications, library creation, and for the use of ECMAScript as a compilation target for other languages.

harmony:generators

harmony:generators are first class croutons that will be represented as objects, which will be encapsulating suspended execution contexts (that is, function activations). Till date, these are under review and can be changed, so we will just take these under consideration to gain knowledge about them.

A few high-level examples would be helpful in better understanding of what the shape of harmony will be after it gets approved.

Since these are unapproved drafts, we will use examples from the ECMAScript parent website.

The reference code to be used in this section can be found at http://wiki.ecmascript.org/doku.php?id=harmony:generators.

The Fibonacci series

The "infinite" sequence of Fibonacci numbers is:

Function* Fibonacci () {

let [prev, curr] = [0, 1];

For (;;) {

[prev, curr] = [curr, prev + curr];

yield curr;

}

}

Generators can be iterated over in loops:

for (n of fibonacci()) {

// truncate the sequence at 1000

if (n > 1000)

break;

print(n);

}

Generators are iterators as shown in the following code:

let seq = fibonacci();

print(seq.next()); // 1

print(seq.next()); // 2

print(seq.next()); // 3

print(seq.next()); // 5

print(seq.next()); // 8

The preceding snippets are very high-level syntax, and there is a fair chance that they will be modified. Generators will be a key element and a remarkable addition to harmony but will take time to fully implement it.

The MEAN stack

Although the MEAN stack is not a new concept, this has provided us the base of everything in JavaScript. It provides you with a JavaScript-based web server in the form of Node.js, a database in the form of MongoDB, which also has JavaScript as the core language, Express.js as a Node.js web application framework, and Angular.js as the frontend element that can let you extend HTML in a more advanced and modern way.

The concepts have been around for a while, but they have the potential to grow beyond imagination. Imagine a full-scale financial application or an entire banking system based on the MEAN stack or controlling industries. The hardware will be utilizing services from this stack, but this will happen in the near future, which is not so late, but still it needs time to fully implement the stack.

The reason I am saying this is because the corporate sector is still reluctant in adopting the MEAN standard or moving towards it and the reason is the level of maturity and financial backup of these open source products. Also, they have to upgrade their existing infrastructure. Whatever the reason, modern day web apps are heavily using this stack for writing lightweight and scalable apps. Let's mark the MEAN stack as our item number one for the future of JavaScript.

Real-time communication in JavaScript

Another powerful feature that has been called the future of JavaScript is real-time communication between two sockets. Before JavaScript, socket programming had been around for so long that every major programming language had its version of reading and writing data using sockets, but with JavaScript, this is kind of a new concept that needs a lot of work at this stage. There are certain ways in which you can implement real-time socket programming in JavaScript, but the most mature way at the moment is by using Socket.IO.

It basically enables a two-way event-based communication in real time that will, in turn, make the communication between two entities possible. It supports a variety of platforms, which includes web browsers, handheld devices, mobile devices, and any other device that has the communication feature enabled. Its implementation is fairly easy and reliable with high quality and speed.

What can we achieve with this? Well, there are a number of possibilities, and it depends on how you can give them a try based on the support Socket.IO has to offer. At this point in time, you can write real-time analytics for your business intelligence or market predictions or trend identification, or you can use it for real-time media streaming from one part of the planet to other using its binary stream functions, or you can use it to monitor you premises from a remote location. All these implementations are available right now and such ideas can be brought to reality by using the functions smartly.

The conclusion is that Socket.IO is one of the most robust real-time communication libraries that you can rely on. Looking at the current trend, we can safely say that real-time communication between devices can be one of the greatest strengths of JavaScript in future. This doesn't really have to happen via Socket.IO; any library that has potential will dominate. It's about the concept of how JavaScript will impress us in the near future.

Internet of Things

Not too long ago, hardware interfacing with devices and machines was only limited to certain mature and developed programming languages, and no one gave any thought to whether JavaScript would be able to stand in the same line as these mature languages. It was a status quo limited to C++ or Java or some other high-level languages, but this is not the case anymore.

With more focus on JavaScript, developers and engineers are now trying to use the power of JavaScript in hardware interfacing. They are overcoming the problems of JavaScript by writing down intelligent code and by utilizing libraries that are already using communication to a device to some extent.

One such effort is called Raspberry Pi. Let's talk about Raspberry Pi and its purpose, then we will take a look at how JavaScript is using it.

Raspberry Pi is a simple credit card-type computer design to learn programming in a very simple and effective manner. It comes with a board that you can call a computer without any peripherals attached. You have to attach the mouse, keyboard, and a screen to bring it to life. It has an operating system mounted on an SD card and is open for experiments. This is portable and you can attach any device to it or program another device using it. It has all the basic elements that a computer must have, but in a very simple, portable, and easy-to-handle manner.

Now, what does it have to do with JavaScript? Well, JavaScript is now everywhere, so its implementation has also started for Raspberry Pi with Pijs.io.

Like you can write in any other language for Raspberry Pi, you can also use JavaScript to write applications of your handheld computer. This JavaScript library will allow you to interact with hardware using JavaScript and program devices for your needs. You can see the library at http://pijs.io/.

As discussed earlier, hardware interfacing is not limited to Raspberry Pi; any other implementations that are out there must be doing the same thing. The core of these lines is to show how powerful JavaScript is becoming and how widely it is accepted. Now, people are considering it for programming their devices, regardless of whether these devices belong to their daily use or commercial use. The future of JavaScript in computer hardware interfacing is very bright and it's growing rapidly.

Computer animation and in 3D graphics

In 1996, a whole new concept of computer generated images (CGI) was introduced in the revolutionary movie, Toy Story. This movie had set new standards in animation and computer graphics. The success of the movie was not just due to its screenplay but also due to the technology used to build it.

In the current time, the field of computer animation has developed from many aspects and is still growing at a rapid speed. So, what does JavaScript have to do with all these advancements? Well, JavaScript is getting readier than ever before to play its role in computer animated and 3D graphics via the Web.

WebGL is an open source JavaScript API for rendering 2D and 3D images and objects. The power of WebGL lies in its extension to nearly every browser by adopting the standards of browsers and their engines. It's highly adaptable and can be used in any modern day web browser to render images as required.

By the virtue of WebGL, it's now possible to write interactive and cutting edge games that require no additional plug-in to run. It will also help in the future to see animated computer modeling with a browser rather than using heavy, costly, and bulky software. It will also help in visualizing information on the go. So, you can see the visual impact of stock prices when they go up and down to other stocks where you have invested.

So far, WebGL has gained support from all the key players of the industry that includes Apple for its Safari; Microsoft for its IE 11 and its later release, the Edge browser; Google for its Chrome browser; and Mozilla for its Firefox. Also, note here that WebGL is the brainchild of Vladimir Vukićević of Mozilla, who released its initial version in 2011.

We can conclude with the fact that JavaScript has planted seeds in animated and 3D graphics as well, and in the near future, this will not only help JavaScript to gain credibility to, but will also bring ease to many developers and engineers who have to learn new languages every time they face a limitation in their current language pack. With a unified language, the output apps will be more interesting.

NoSQL databases

There was a time when knowing RDBMS was a must for all developers, especially for those who were working on database-driven applications. The expectation was that you must know what primary keys were, what joins were, how to normalize databases, and what entity-relationship diagrams were. However, slowly this scenario is fading and a new concept of NoSQL is emerging in today's world, where vast data-driven applications are still in play.

Before we move forward, let's talk about why engineers are focusing on non-RDBMS technologies. The reason is simple. Data-driven applications have grown in a tremendous way and they're generating terabytes of data around the world in every hour of the day. To process such data and get the desired result is not an easy task. Database Administrators (DBAs) write a query and execute it to fetch the data from distributed repositories of databases, they have to wait for several hours to know whether the results are printed on their screen or a slight error in placing the operator has destroyed all their efforts. This is because of the way RDBMS was designed, but in today's modern world, such delays and computing times cost you a fortune and your reputation.

What is the alternative then? NoSQL databases! In an earlier section of this chapter, we already saw that MongoDB played a key role in the MEAN stack. However, it's worth giving MongoDB a few more lines here as it's our candidate for the future growth of JavaScript.

What is MongoDB? It's a document-oriented NoSQL database with cross-platform adaptability with JSON such as documents. Till February 2015, it was the fourth most popular DBMS in the world and is considered to be the most popular data store in the world.

Why have we listed MongoDB in our candidates for future JavaScript growth? Simply because it's JavaScript-based and you can write scripts within its console in pure JavaScript. This makes it a highly adaptable DB technology based in JavaScript. The way it's progressing, it will not only obsolete current scenario of RDBMS but also will do wonders when combined with the rest of the MEAN stack or hardware interfacing or the Web or with Socket.IO.

In any shape, MongoDB will help the rest of the applications to grow in the future and also transform the existing RDBMS to more accessible and quick responder engines.

Summary

In this chapter, we learned that JavaScript is a game changer and that it has a bright future ahead. JavaScript has a great tendency and adaptability, which will lead it to the next level of usage in almost every domain of computer science. The possibilities are limitless, and the sky is the limit for JavaScript. In the near future, JavaScript will dominate every other programming language due to its adaptability, acceptability, and contributions of thousands of developers and committed software giants.

With this, we come to the end of this book.

Let's recap what we learned in this book. At the beginning, we took a deep dive into what JavaScript is and from where it began, what the structure of JavaScript is and how different browsers are using it. We also saw different programming models and the one that is being used by JavaScript.

Then, our journey took a bend towards the core of this book, Promises.js. We learned a great deal about the basics of promises that has taken us towards the advanced usages of this concept. We then saw it with respect to different technologies and also samples the code to clear out any ambiguities.

So, all in all, this book is not only about promises in JavaScript, but it has a solid overview of the history, implementation, and usages of JavaScript and promises. With this book, you can not only become a master in promises, but also retain a unique level of understanding, thus implementing this concept in much brighter and numerous ways.

Happy learning!





All materials on the site are licensed Creative Commons Attribution-Sharealike 3.0 Unported CC BY-SA 3.0 & GNU Free Documentation License (GFDL)

If you are the copyright holder of any material contained on our site and intend to remove it, please contact our site administrator for approval.

© 2016-2026 All site design rights belong to S.Y.A.