JavaScript with Promises |
Author: Daniel Parker Are you confused by promises? Perhaps 94 pages is all it takes to be unconfused. Promises is a way of simplifying asynchronous behaviour in JavaScript but, and here is the paradox, many programmers struggle to understand what it is all about. This book aims to clear up the mess and leave you confident in your use of Promises. The first thing to say is that, while some polyfix libraries do get a mention, this book is mostly about the Promises standard as implemented in ES6. Given that there are many cases where you have to support browsers that are not up-to-date this is still a limitation on how easily you can make use of Promises. The first chapter sets the scene with a good explanation of the problem Promises is trying to solve. This introduces the idea of "run to completion" and the event loop. Both of these are essential ideas it you are going to have any hope of understanding what is going on in JavaScript, but many programmers only have a very vague idea of this aspect of the language because they aren't really part of the language. From here Chapter 2 introduces basic Promises. To be honest this isn't very basic. After a very small simple example, things progress very quickly and we are faced with more advanced uses - chaining, multiple consumers and error propagation. This isn't unreasonable as, if you are going to use Promises to wrap the use of a single callback, there isn't too much advantage. To see why Promises is a good idea you really do have to look at more complicated use cases. These are all clearly, but briefly, described. My only reservation is that it might be too brief if you are struggling to see the advantages of Promises. For example, a key advantage is the way errors propagate and this is treated here in just two pages - there is a complete chapter on the topic later, however. Chapter 3 is titled "Working with Standard Promises", but it is really a sort of cookbook of situations in which you can use Promises. It covers dealing with a conditional and a loop and other common programming structures. Chapter 4 looks a using libraries and frameworks, but really it only looks at the Bluebird Promise Library. It does mention jQuery and the well-known problems with its implementation of Promises. However, it doesn't really explain what is wrong and doesn't mention that the problem is put right in jQuery 3.0
Chapter 5 is entirely dedicated to explaining error handling. Much of the message is the idea that functions that return Promises shouldn't throw errors, they should reject the Promise. This is perhaps the most important chapter in the book - read it carefully. The final chapter is a collection of ways in which new ES6 features work well with Promises. A lot of what is explained here can be regarded as clever programming. In particular, we have the use of generators as a way of writing asynchronous code that looks like synchronous. The problem with this is that this approach is essentially a polyfil for the forthcoming async and await operations in the next version of JavaScript. And this brings me to a small potential problem with the whole book, or rather its subject matter. As a concept, Promises is difficult for the average programmer to understand and the fault lies with the very idea. In practice, Promises are sophisticated and using them properly requires the level of maturity and knowledge that Danail Parker sets out to deliver. In the main he succeeds. If you want to know how Promises work and how to use them then read this book very carefully, think hard about what it says and them think really hard when you start to use Promises in any sort of creative way. Or you could just wait for async and await which you will understand immediately and write good code without even trying. A good book but not a good technology.
To keep up with our coverage of books for programmers, follow @bookwatchiprog on Twitter or subscribe to I Programmer's Books RSS feed for each day's new addition to Book Watch and for new reviews.
|
|||
Last Updated ( Tuesday, 15 March 2016 ) |