JavaScript Promises: A Journey To The “Promise Land”


Promises certainly aren’t the holy grail of asynchronous flow control in JavaScript. There are no silver bullets, after all. Promises are a tremendously important concept, and one that you need to understand, though. They’ve been around for a few years, have gone through various standards and APIs and are finally being added to JavaScript, proper. There are countless resources around the web these days, showing how promises work and what you can do with them. But in all this, it’s easy to get a bit lost. So I want to take you down a short journey – one that I’ve already travelled and documented fairly well. But here, I’ll summarize my journey of re-inventing promises (by accident), learning what they are and how I came to use them on a daily basis, for a while.

The TL;DR Of My Promises Journey

I was working with a client a few years ago and we had a problem. We needed to load up a single Backbone model from a collection, but didn’t have a guarantee of the collection being loaded yet. So I created a way to guarantee callbacks firing. What I didn’t realize at the time, was that promises did exactly this. It was shortly after writing that post that someone introduced me to jQuery Deferred objects. I quickly rewrote my callbacks class to be a thin wrapper around deferred, and moved on. A short time later, I found myself working on a contract for Microsoft Patterns & Practices group, building a Windows 8 / WinJS application. It turns out Windows 8 / WinJS apps are full of promises, and you really do have to understand them if you want to build anything with Win8 / WinJS. This was my first in-depth exposure to “real” promises, though I had become familiar with the term by now, and knew that jQuery’s deferred weren’t quite a “promise” (at least, by the standards at the time. maybe that has changed?).

My love affair with promises continued in Backbone for a while. And once I started doing more work in NodeJS, I started doing more work with promises using Q and RSVP. I noticed that my NodeJS experience was a little closer to my Win8/WinJS experience, too – at least for the use of RSVP. Others, though, provided very different APIs in comparison to both jQuery and WinJS. So I wrote up a small complaint about the inconsistencies in promise libraries, stating my preference for the A+ spec that RSVP was implementing. In that post, I learned that JavaScript is getting official support for promises. This is exciting news, to me. It’s good to see such a great tool being added to the language directly. I’m hopeful that this will improve the state of promises, in general, and help us move forward with other tools and technologies that are built on top of them.

My Promises Screencasts

Having learned quite a bit about promises and spent a significant amount of time using them in various forms, I created a couple of screencasts around promises – one of which is freely available on Youtube and has a blog post over at Telerik. This screencast shows how to clean up nested callbacks with promises. It’s a great introduction to the things that promises let us do, and shows one option for getting out of callback hell / the christmas tree of doom.

The other is a paid screencast on my WatchMeCode screencast series – Episode 13: Promises From The Ground Up.

NewImage

This screencast takes you roughly through the same journey that I had when I first needed a promise. It shows some very basic, async code (facilitated with setTimeout in this case) and then walks through a very minimal (incomplete) promise implementation.

The goal is to break down promises in to what they really are – callbacks that are stored for later execution. If you’ve been beating your head against the wall of promises, trying to understand how they really work (even if you’ve used them successfully!), then this screencast is one that you’ll want to pick up. Tearing down the illusion and revealing the person behind the screen is a powerful way to move from the realm of “magic” (that which we don’t understand) to “technology” (that which we do understand).

Heroku And SSL: Fixing “This site’s security certificate is not trusted!” on Android and other devices