Chapter 6. Combining ECMAScript 6 Features with Promises

ECMAScript 6 has a number of language features that complement promises. This chapter shows how destructuring, arrow functions, iterators, and generators simplify your promise-related code. However, this is not a full explanation of these features or ES6. It is merely a starting point for taking advantage of ES6 in your code.

The new syntax that these features require causes errors in JavaScript environments that do not support them. Unlike the Promise API that is unobtrusively polyfilled, code that uses the new syntax must be modified in order to run in older environments. You can automate the modification by transpiling the code into something equivalent that runs in older environments. However, multiple JavaScript environments such as Google Chrome and Mozilla Firefox already support some of these features, such as generators. The ECMAScript 6 compatibility table maintained by Juriy Zaytsev (a.k.a. kangax) on GitHub is a good place to see which ES6 features are available on your target platform.

Destructuring

Destructuring provides a syntax for extracting values from arrays or objects into individual variables. Instead of writing individual assignment statements for each variable, destructuring allows you to assign the values for multiple variables in a single statement. Examples 6-1 and 6-2 present destructuring using an array and an object.

Example 6-1. Array destructuring
var numbers = [10, 20];
var [n1, n2] = 

Get JavaScript with Promises now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.