Examples Gallery

Jurie Horneman edited this page Apr 14, 2016 · 19 revisions

Q + Mongoose, as an example of adapting Node code. Based on this Stack Overflow question.

Q + node_redis, as an example of adapting the node_redis operations to Q (Coffeescript)

Flickr API Experiments, as an example of adapting jQuery Ajax promises for a JSONP API. Uses deferreds, the get utility, Q.all, and chaining.

Basic Chaining Example, showing how promises compose. Also illustrates Q.nfcall as an alternative to using deferreds when working with Node.

Simple Chaining Example, briefly explains how chaining works.

Promises in a UI Context, a somewhat extensive and complicated "real world" example from @domenic's work at Barnes & Noble.com. Includes some helper functions to create alert/confirm/prompt dialogs that communicate their results via promises; adapts jQuery Ajax promises; and shows how to intercept and "re-throw" rejections at system boundaries.

Using Array.reduce to make your own Q.all. This doesn’t work as well as Q.all or Q.allResolved, but I found it very instructive in discovering how promises can be composed.

A very simplistic HTTP proxy

A simplistic HTTP "queue" application Where POST puts a request’s actively streaming body into the queue and GET pipes the request’s body to the response’s body.

How to adapt the XHR interface to an HTTP.read(url) that returns a Q

Control flow emulating "break" out of a loop of promises

Retry a some times with a delay between attempts

Old Narwhal code that illustrates functional composition of promises But definitely would not run today without some modifications.

XMLHttpRequest wrapped into a promise

Concurrent Promises using allResolved, spread and all. Shows how one would go about having a bunch of functions that can execute together while another waits before executing, and how this could be done at the beginning of a chain, or during a chain. Plus, shows those concurrent promises piping through their values to the next function in the chain, together.

Using promises as a cache and resource sharing feature. Shows how to store results from long-running functions so the result can be used by many callers. Useful for web services.

Using promises as way to abstract the IndexedDb API into a usable interface. Wraps each async call to an IndexedDb instance/transaction/cursor or update with a promise. This allows the user to think only about the desired sequence of events, as opposed to the actual async sequence of events under the hood, without needing to worry about the order of operations.

Q.js examples - Small snippets to understand various api methods in Q.js

MapReduce example - Using Q.all to merge the results of an arbitrary number of promises together. One promise generates rows of data to be worked on concurrently, a second promise simulates additional work per row, and Q.all combines them at the end.

Examples based on scenarios described at Q documentation.

Accessing jQuery error information when using Q - How to access all of jQuery's AJAX error information when something goes wrong.