ES6: Return of Javascript

This is the third and final installment of my blog trilogy on ES6 based on the information presented by Aaron Frost on Pluralsight. If you haven’t seen the talk, check it out! In this episode, we are going to take a glance at promises, generators, and modules!

Promises

Promises are one of the most exciting features being presented in ECMAscript6. If anyone has ever worked heavily in asynchronous code, then you are probably very familiar with Callback Hell, let me show you what it looks like:

Callback Hell commonly occurs during chained asynchronous functionality. Without setting the callback properly, the next step would get called before the previous step completed, usually causing an error or crashing the program.

Promises come to the rescue by offering an agreement to the app that promises to execute once the previous call has finished executing, it allows us to completely avoid callback hell and makes code run a lot more efficiently. It works by creating an event handler, and when it recieves a result or response, fires the event so that it gets added to the event loop.

Promise Constructor

The promise takes a resolve which is a callback function that is executed upon success and a reject that is executed upon failure. The promise object that is returned from the constructor can be in 1 of 4 states:
1. fulfilled: successfully resolved
2. rejected: rejected
3. pending: still working, neither resolved or rejected
4. settled: fullfilled or rejected

No matter how long this takes to run, the following code will only be executed upon its completion, and without locking up the program.

Promises can also be simplified by wrapping it into an outer function, here is a use case with an asynchronous function in jQuery

Promises.all

The new ES6 promises also offer a feature that waits until all currently pending promises are complete before continuing. This is perfect for complex API or Database calls.
utilizing the get method from the previous example

When returning items in a promise, the return statement gets passed to the next .then

Promise Methods

  1. Promise.all(iterable); : waits until all pending promises are complete
  2. Promise.race(iterable); : waits until 1 promise is complete even if others are pending
  3. Promise.reject(reason); : creates a pre-rejected promise
  4. Promise.resolve(value); : Create a pre-resolved promise

Generators

Generates offer an awesome way to control the flow of a program. It offers the yield keyword, that allows a function or process to pause mid-process to see if there are any items waiting in the event loop such as an event handlers that has been called

The * signifier sets a function up as a generator, when calling gen.next(), an object is returned, the first key in the object is the value, which is the return statement provided by the initial yield keyword, the second key is done, which will be false if there are other yields in the function, or true if there are no more yield statements left.
Now we call .next() again

Iterators

The Generator contains an iterator so we can use the for of loop. If you are unfamiliar with iterators, check out my last blog post here. It is very important to note, that only the yield statements are iterable. The iterator will operate as long as done does not equal true, and done equals true once there are no more yield statements.

Passing in Values

Pretty cool huh!? Let’s walk through this problem step by step so we can gain a deep understanding of what is happening here.

So let’s rewrite the first line with the new variable applied

next we have the first yield, no value is applied

The app stays at this state until the next .next is applied. This yield comes with a variable of 12 which replaces the previous yield’s value

the final .next is called this time with a variable of 13, just like before, the 13 replaces the previous yield.

Finally we have three variables left, from all the .next parameters passed in.

As you can see, there is a ton of power in yielding and passing values. It gives us enormous control over our functions and the way we run applications.

Generator Expressions were pushed to ES7

Modules

ES6 brings in some much needed support for modules. Those who have worked with Node or are familiar with CommonJS will not find anything surprising here. However getting modules working out of the box in javascript is going to be an incredible feature and make the use of front-end frameworks significantly cleaner. If you’ve ever tried using jQuery or some tool made with jQuery within the Angular framework, you probably ran into a huge problem. The $ sign which is jQuery’s main signifying tag is also heavily used in Angular. There are work arounds, but with modules, these workarounds will no longer be necessary. Let’s dive in!

Explicit Imports

Import All

Export or import aliases

Or

When can I use ES6 ???

You can check out this page here, which tracks which browser and repl has adopted the various features of ES6. Although, realistically you should be using Babel to transpile your ES6 code for any browser

Originally published at questhenkart.com on June 9, 2015.

Software Architect