A Glossary of National Animals

In Afghanistan you may be stunned How a Snow Leopard can have you outgunned. Known locally as the Ounce, Like a bolt of lightning he can pounce. You must move quickly, it makes sense, Lest your…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Test Your Promises

Test-Driving JavaScript Applications — by Venkat Subramaniam (28 / 108)

👈 Client-Side Callbacks | TOC | Wrapping Up 👉

Now let’s explore testing for promises in code running within Node.js. The same techniques work for client-side code that use promises as well. We’ll start with a short introduction to promises and then dive into testing code that uses promises.

Before digging into testing promises, let’s review how promises works — I promise to keep it short.

Traditionally asynchronous functions have used callbacks. But callbacks have a few drawbacks. Callbacks have to be registered before the asynchronous functions generate any response. For example, when using an instance of XMLHttpRequest, calling the send function before registering a callback with the onreadystatechange property may result in some events being missed. Also, it’s hard to compose callbacks — the code become overly verbose and cumbersome when a callback in turn has to invoke other asynchronous functions and register other callbacks.

Promises don’t have those issues and that’s one of the reasons they’re gaining in popularity. Functions that use promises immediately return a Promise object in response to calls. They then respond to the caller through the Promise object.

Promises more clearly express the intent of asynchrony than callbacks do. Here’s a figure that shows the propagation of promises through a series of function calls.

images/async/promises.png

Code that uses promises may form a chain of then and catch calls. Either the top path or the bottom path in the chain is taken, depending on whether a promise is resolved or rejected. If the Promise is resolved, then the resolved part of the then function will transform the resolved value and pass it on, as a Promise, to the next receiver in the chain. On the other hand, if the Promise is rejected, then a series of catch functions transform the rejected reason through the call chain. This chain may also optionally include the rejected part of the then function.

Add a comment

Related posts:

Chapter 10 Source Control Management

Learn the critical tips and techniques to make using Xcode for the iPhone, iPad, or Mac easier, and even fun. Explore the features and functionality of Xcode you may not have heard of. Go under the hood to discover how projects really work, so when they stop working, you'll know how to fix them. Explore the common problems devel

Chapter 9 Writing Your Own Methods

Printed in full color.For this new edition of the best-selling Learn to Program, Chris Pine has taken a good thing and made it even better. First, he used the feedback from hundreds of reader e-mails to update the content and make it even clearer. Second, he updated the examples in the book to use the latest stable version of R

12.1 Intercepting Methods Using GroovyInterceptable

Groovy brings you the best of both worlds: a flexible, highly productive, agile, dynamic language that runs on the rich framework of the Java Platform. Groovy preserves the Java semantics and extends the JDK to give you true dynamic language capabilities. Programming Groovy 2 will help you, the experienced Java developer, lea