Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
the beauty of promises
// Before introducing promises, each of these functions required a callback to be passed in to it.
// This leads to deeply nested structures and the arrow-anti-pattern, creating more visual complexity
// and reducing the maintainability of this code
Tiles.buildThumnails(images, function(thumbnails){
Tiles.transmogrify(thumbnails, function(notification){
// After introducing promises, the methods can be chained together easily,
// reducing the nesting and the visual complexity of the code. This allows
// the code to be more easily understood, and therefore easier to maintain.
// Each of these functions still receives the same first-position parameter,
// but none of them needs to receive a second parameter as a callback. They
// each return a promise instead of using callbacks.
.then(function (notification) {

For these cases I find reduce can be quite nice as well... taken from the Q docs which is an awesome promises implementation. Their docs are excellent for understanding the implementation of promises too.

var funcs = [foo, bar, baz, quux];

return funcs.reduce(function (soFar, f) {
    return soFar.then(f);
}, Q.resolve(initialVal));

now that's an interesting approach! i haven't seen that before. :)


I wish I could take credit :-) take a look at this documentation for some great uses of promises.


With the async.js library and node-style callbacks this would be

  updater.update], function (error, results) {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.