Asynchronous JavaScript is everywhere, whether you’re using Ajax, AngularJS, Node.js, or WebRTC. This practical guide shows intermediate to advanced JavaScript developers how Promises can help you manage asynchronous code effectively—including the inevitable flood of callbacks as your codebase grows. You’ll learn the inner workings of Promises and ways to avoid difficulties and missteps when using them.The ability to asynchronously fetch data and load scripts in the browser broadens the capabilities of JavaScript applications. But if you don’t understand how the async part works, you’ll wind up with unpredictable code that’s difficult to maintain. This book is ideal whether you’re new to Promises or want to expand your knowledge of this technology.Understand how async JavaScript works by delving into callbacks, the event loop, and threading Learn how Promises organize callbacks into discrete steps that are easier to read and maintain Examine scenarios you’ll encounter and techniques you can use when writing real-world applications Use features in the Bluebird library and jQuery to work with Promises Learn how the Promise API handles asynchronous errors Explore ECMAScript 6 language features that simplify Promise-related code
“In short, a callback is a function provided to other code for invocation.”
“As long as you have a reference to a function, you can use it as a callback.”
“window. requestAnimationFrame(). Its callback is invoked between browser repaint intervals”
“Synchronous code can be easier to understand because it executes in the order it is written.”
“In JavaScript, callbacks are used to manage the execution order of any code that depends on an async task.”
“Remember that a promise serves as a placeholder for the result of an operation.”
“When a promise is no longer pending it is said to be settled. This is a general term indicating the promise has reached its final state. Once a pending promise is settled the transition is permanent. Both the state and any value given as the result cannot be changed from that point on. This behavior is consistent with how operations work in real life. A completed operation cannot become incomplete and its result does not change.”
“promise is a placeholder for the result of one attempt of an operation”
“The immutability of a settled promise makes code easier to reason about. Allowing the state or value to change after a promise is fulfilled or rejected would introduce race conditions. Fortunately, the state transition rules for promises prevent that problem.”
“Since the reject function transitions a promise to the rejected state, why does the resolve function transition a promise to a state called fulfilled instead of resolved? Resolving a promise is not the same as fulfilling it. When the argument passed to resolve is a value, the promise is immediately fulfilled. However, when another promise is passed to resolve, such as promise. resolve (otherPromise), the promises are bound together. If the promise passed to resolve is fulfilled, then both promises will be fulfilled. And if the promise passed to resolve is rejected, then both promises will be rejected. In short, the argument passed to resolve dictates the fate of the promise. Figure 2-2 shows this process.”
“The resolve and reject functions can be called without an argument, in which case the fulfillment value or rejection reason will be the JavaScript type undefined. The Promise API also provides two convenience methods (see Example 2-7) for creating a promise that is immediately resolved or rejected: Promise. resolve() and Promise. reject().”
“cases. The resolver function passed to the Promise constructor executes synchronously. And all callbacks passed to then and catch are invoked asynchronously.”
“Rejections and errors propagate through promise chains. When one promise is rejected all subsequent promises in the chain are rejected in a domino effect until an onRejected handler is found. In practice, one catch function is used at the end of a chain (see Example 2-12) to handle all rejections. This approach treats the chain as a single unit that the fulfilled or rejected final promise represents.”
“Promises are also rejected when an error is thrown in a callback passed to then or in the resolver function passed to the Promise constructor.”
“any value, including undefined, can reject promises, we recommend using an error object. Creating an error can capture the call stack for troubleshooting and makes it easier to treat the argument the catch handler receives in a uniform way… Using JavaScript Error objects to reject promises can capture the call stack for troubleshooting and makes it easier to treat the argument the catch handler receives in a uniform way.”
“The Promise global is a constructor function that the new keyword invokes. The Promise global creates promise objects that have the two methods then and catch for registering callbacks that are invoked once the promise is fulfilled or rejected”
“The promise.then() method accepts an onFulfilled callback and an onRejected callback. People generally register onRejected callbacks using promise.catch() instead of passing a second argument to then”
“promise.catch(onRejected) returns promise”
“The promise.catch() method accepts an onRejected callback and returns a promise that the return value of the callback or any error thrown by the callback resolves or rejects, respectively. That means any rejection the callback given to catch handles is not propagated further unless you explicitly use throw inside the callback.”
“The Promise.resolve() function is a convenience function for creating a promise that is already resolved with a given value. If you pass a promise as the argument to Promise.resolve(), the new promise is bound to the promise you provided and it will be fulfilled or rejected accordingly.”
“Promise.reject([reason]) returns promise”
“The Promise.reject() function is a convenience function for creating a rejected promise with a given reason.”
“Promise.all(iterable) returns promise”
“The Promise.all() function maps a series of promises to their fulfillment values. It accepts an iterable object such as an Array, a Set, or a custom iterable. The function returns a new promise fulfilled by an array containing the values in the iterable. Corresponding fulfillment values in the resulting array replace any promises contained in the iterable. The new promise that the function returns is only fulfilled after all the promises in the iterable are fulfilled, or it is rejected as soon as any of the promises in the iterable are rejected. If the new promise is rejected it contains the rejection reason from the promise in the iterable that triggered the rejection. If you are working with a Promise implementation that does not understand ES iterables, it will likely expect standard arrays instead”
“Promise.race(iterable) returns promise”
“The Promise.race() function reduces a series of items to the first available value. It accepts an iterable and returns a new promise. The function examines each item in the iterable until it finds either an item that is not a promise or a promise that has been settled. The returned promise is then fulfilled or rejected based on that item. If the iterable only contains unsettled promises, the returned promise is settled once one of the promises in the iterable is settled”
“Keep these three points in: 1) A promise is a placeholder for a value that is usually the result of an asynchronous operation. 2) A promise has three states: pending, fulfilled, and rejected. 3) After a promise is fulfilled or rejected, its state and value can never be changed.”
“As a general rule, any function that uses a promise should also return a promise. When a promise is not propagated, the calling code cannot know when the promise is fulfilled and thus cannot effectively perform work after the fact. It’s easy to ignore this rule when writing a function whose caller does not care when the async work is finished, but don’t be fooled. It’s much easier to return a promise that initially goes unused than to retrofit promises into a series of functions later on.”
Daniel Parker's JS with Promises does a great job of introducing promises in a rich with code examples manner. Then it expands into more advanced promises scenarios that are mostly showcased as 'how to do it naively' and 'how to do it right', which in my opinion is the right way to approach this.
The promise libraries featured in the book are mostly Promises/A+ compliant ones - Bluebird, Q and ES6 promises. Of course the black sheep jQuery is there too for 'finger pointing' purposes on why not to use jQuery promises in your projects (if possible). Ok I may have exaggerated, but really jQuery promises are not Promises/A+ spec compliant and are definitely not there to stay.
Since it's 2015, every JS book has to have a ES6 features chapter. Of course JS with Promises has one too. However it's not just something put there for no reason except the JS ES6 hype, but it's actually well integrated with the concept of the book. The ES6 chapter starts with showing pre-ES6 ways to do things and how to do them with ES6 and it all revolves around promises. It's definitely worth reading since it makes using Promises even more fun. If anyone really wants to dive deep in ES6 - Understanding ES6 by Nicholas C. Zakas goes through every ES6 feature in more details.
Overall it has the right balance between code examples and explanations. The book is enough to make you friends with Promises and start to utilise them "properly" and not in a naive manner as the book showed on multiple places to teach the reader.
I'm certain that JS with Promises can be considered a must read for JS developers. It's aimed for a bit more advanced JS developers, but it's definitely worth reading by everyone at some point.
The book is a bit dated. ES7 introduces async and await (both available in node and mentioned in a few sentences), and Observables. Moreover, the book is needlessly complex in many areas: the section on chaining is obtuse. Worse, the section on ES6 is misplaced and too abbreviated: there is no mention of arrow-syntax changing the context of 'this'. Some things seem kind of obscure even to me, such as when conditionally chaining promises is a "Good Idea" especially, after the chapter right before reiterates why it's a bad idea: conditionally chaining a promise is essentially the same as chaining it synchronously with a no-op function. And, on the subject of chaining, explaining `array.reduce()` seems to waste valuable space when the book is only 80 pages.
The author should focus on the subtitle "Managing Asynchronous Code." To that end, it should be better paced. It starts off really well, and it goes downhill fast. There are two use cases of asynchronicity: client and server. Both of these have different demands and tools: there is no mention of server side libraries Co or Koa (or Koa v2), or Thunks. There is also no mention of client-side libraries that use Bluebird (or promises) and the ilk.
It's not very good at describing the ecosystem on the server, or the client. And, it is near devoid of advice -- good or bad. Another book of about the same size, JavaScript: The Good Parts, is fixed on making suggestions about implementation and practice. If this book is to stay the same size, it should become opinionated and reorient itself. It is lacking far too much for it to be authoritative on anything or introductory for asynchronous Javascript.
The author does a good job summarizing the benefits of using promises over traditional asynchronous callbacks in JS. The example code in Chapter 3 ("Working with Standard Promises)") is probably the most helpful part of the book since it covers common programming scenarios.
Unfortunately, the explanations of the standard Promise API were sometimes difficult to follow (like a lot of technical documentation).
Overall, I wish I had read this book _before_ i started working with promises because it's a good introduction, but there's not a lot more to it.
Everything you need to know about promises to work effectively with them. The book is quite short and easy to read, and at the same time it goes deep enough into the topic of promises and async programming in JS at all.
This is a good book describing Promise in Javascript and mentioned two or three libraries providing the Promise feature and highlight some of the differences between them.
Ok, this is the book (or at least a book) I was looking for to learn and resolve problems I'm having with Asynchronous runtime execution and passing data between asynch code and the mainline sequential event loop. I've been told it's a non-trivial problem which is often left as an exercise for the newly minted Javascript programmer. It Should Not be. Fortunately this author goes into some detail describing the problem and the evolving Javascript language which "solved" it. The book and its description is complex but the author provides a detailed description (from what a 1st read provides). I look forward to reading more books on the subject and returning to this book to absorb the intricacies of asynch code and promises in Javascript.
Good information, but some of the examples were a bit abstract and would have been better if they included more real world scenarios. I also found that the chapter ordering could have been better. It started with some basic concepts, moved on to some more advanced concepts, and then went back to basics at the end of the book.
Must read if you're starting to work with promises
If you've been working with promises for awhile though, it might not offer much to learn but might solidify your understanding of how some parts of the promise APIs work. Really easy to read with small and clear examples to help you understand promises.