Saturday, November 15, 2014

Book Review: JavaScript Promises Essentials

Because JavaScript Promises are a trendy web development topic that I know very little about, I accepted Packt Publishing's offer to review the electronic (PDF) edition of Rami Sarieddine's JavaScript Promises Essentials. Sarieddine introduces this book on his blog post JavaScript Promises Essentials. The subtitle of the book is, "Build fully functional web applications using promises, the new standard in JavaScript." JavaScript Promises Essentials is a short book with six chapters and a preface covering approximately 70 pages of substantive content.


The "Preface" of JavaScript Promises Essentials states that the book is "a practical guide to the new concept of promises" that "presents a single resource replacing all the scattered information out there about the topic" (I added the links). The Preface introduces the concept of promises and why they are useful.

JavaScript Promises Essentials's Preface provides very brief summaries of each of its chapters before covering what readers of the book need when reading it. The author states that readers "just need an HTML and JavaScript editor" and recommends three candidates: Microsoft Visual Studio Express 2013 for Web, Microsoft's WebMatrix, or jsFiddle. The Preface also suggests that JavaScript Promises Essentials is designed for "all developers who are involved in JavaScript programming" with special appeal for those "interested in learning about asynchronous programming in JavaScript and the new standard that will make that experience much better."

Chapter 1: JavaScript Promises – Why Should I Care?

The initial chapter of JavaScript Promises Essentials provides a brief summary of JavaScript and how it became a popular programming language, especially in web browsers. The chapter talks about how web development and user experience has progressed. The chapter provides a basic overview and examples of applying JavaScript event handlers and callback functions.

Chapter 1 also includes a section "Why should I care about promises?" that starts with a definition of promise quoted from the Promises/A+ web page: "A promise represents the eventual result of an asynchronous operation." After presenting this simple introductory definition, the chapter then moves onto further elaboration on the nature of promises (incuding having state and being immutable). This section provides a nice overview of what JavaScript promises are and the syntax for them.

Part of Chapter 1 demonstrates how complicated chained callbacks can become and how much easier they are to read when presented as promises instead. The chapter concludes with an interesting summary: "Promises are a pattern that allows for a standardized approach in asynchronous programming" and enables "developers to write asynchronous code that is more readable and maintainable."

Chapter 2: The Promise API and Its Compatibility

JavaScript Promises Essentials's second chapter covers "current browser support for the promises standard" and "JavaScript libraries out there that implement promises and promise-like features." Coverage of the Promises API begins by contrasting the Promises/A+ specification with the Promises/A specification and focusing on additions, removals ("omissions"), and clarifications. This discussion gets fairly deep into the descriptions of the rules to be supported by Promises/A+ implementations.

The second chapter's discussion rises back to a higher level of discussion on "browser support and compatibility" for promises. This includes a reproduction (in color in electronic edition) of the Promises portion of the "ECMAScript 6 compatibility table" provided by kangax. The author introduces Remy Sharp's concept of a polyfill ("piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively") and then references a particular polyfill for web browsers that don't support promises natively.

Chapter 2 moves onto coverage of "libraries with promise-like features" and provides descriptions of libraries that implement "the promises concept" (though not necessarily ECMAScript 6 or Promises/A+ compliant). The briefly described libraries are kriskowal/q, tildeio/rsvp.js, cujojs/when.js, the then collection, and jQuery.Deferred().

Chapter 3: Chaining of Promises

The third chapter of JavaScript Promises Essentials revisits the discussion from the first chapter about the disadvantages of using chained callbacks in JavaScript and how promises address those callbacks. The author states that promises provide a "straightforward, standard mechanism to chain asynchronous operations." This short chapter provides fairly detailed introductory examples of chaining promises.

Chapter 4: Error Handling

The fourth chapter of JavaScript Promises Essentials begins with a brief explanation of the challenges of dealing with asynchronous error handling in JavaScript before looking at how promises address these challenges. Discussion and examples illustrate use of then, catch, onFulfilled, and onRejected.

Chapter 5: Promises in WinJS

The focus of Chapter 5 of JavaScript Promises Essentials is on WinJS, an implementation of Promises. The chapter briefly introduces WinJS and advantages of using WinJS. This discussion includes mention of the increasing number of environments supporting WinJS and the open sourcing of WinJS. The WinJS namespace is introduced with an introduction to its basic features including xhr and Promise.

Unsurprisingly, the fifth chapter emphasizes the WinJS.Promise object. The three main components of a WinJS.Promise object (Constructors, single Event, and Methods) are discussed. The discussion on WinJS.Promise's methods contrasts the then and done methods. This section also explains that WinJS is compliant with Promises/A, but not tested for compliance with Promises/A+ and explains practical ramifications of this.

JavaScript Promises Essentials's fifth chapter includes an example (including color screen snapshot) of using Visual Studio to apply a WinJS promise. The example includes use of WinJS.Utilities.ready and WinJS.xhr. I like that the author recommends for those interested in trying out WinJS. For those who are not using WinJS, this chapter could probably be skipped, though there are some general ideas of promises that come out in the chapter even as it discusses WinJS specifically.

Chapter 6: Putting It All Together – Promises in Action

Chapter 6 of JavaScript Promises Essentials begins by explaining and demonstrating creation of "a minimal JavaScript library that implements promises." I found this final chapter of JavaScript Promises Essentials to be one of the more interesting as the explanations of building a promise implementation in JavaScript definitely provided concreteness that helped solidify my understand of JavaScript promises.

Once the simple promises implementation is built, the chapter moves onto using that implementation in examples. The chapter's summary also serves as a summary of the entire book.

General Observations

  • JavaScript Promises Essentials provides a general overview of JavaScript promises fit for general development. However, it is is particularly relevant for Windows developers with a chapter focused on a Windows-specific implementation of Promises.
  • Some of the text in the book is a bit choppy and some of it I'm not even sure conveys what was originally intended. Here is an example: "Callbacks are lightweight since we need to add extra libraries."
  • There are a few illustrations in JavaScript Promises Essentials. Some of them are in color and some are in grayscale even in the PDF. In one case, a flow chart in chapter 4, the text references green and red colors but the illustration is grayscale.
  • The code listings are black font on white background with proper indenting but without line numbers or color syntax highlighting.
  • Because book reviews are opinionated by nature, it can be useful to read several different reviews for different opinions. Other reviews can be found (or likely soon will be found) at the following sources:


JavaScript Promises Essentials covers one of the trendiest concepts (promises) in one of today's most popular languages (JavaScript). The book is no longer than it needs to be and provides a nice overview of implementing and using promises in JavaScript. It is especially well suited for Windows developers with one of its six chapters entirely focused on WinJS, but the other five chapters are of benefit to anyone who wants to know more about applying promises in JavaScript.

The information in JavaScript Promises Essentials is generally conveyed well, though there are some awkward (and sometimes too lengthy) sentences. Some of the book's strengths include coverage of the Promises/A and Promises/A+ standards, coverage of the current state of promises in JavaScript, and description of current JavaScript Promises implementations and libraries.

No comments: