Skip to content

Instantly share code, notes, and snippets.

@melissamarima
Forked from robotlolita/promises.md
Created November 2, 2015 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melissamarima/14fc9a6d67919ffd1d74 to your computer and use it in GitHub Desktop.
Save melissamarima/14fc9a6d67919ffd1d74 to your computer and use it in GitHub Desktop.
Promises: replacing dependency on time, by dependency on your data.

So, promises are the big thing now in JavaScript and all that shit. Though they are a fairly old concept. In short using promises means you're giving up on your dependency on time and mutable state, and buying into dependency on your data, regardless of time, or structure, or any of those unnecessarily complicated things.

First, let's build a conceptual understanding of promises, and then I'll show how this maps to the particulars of the Promises/A+ specification. A promise is a placeholder for a value. Instead of saying what your value depends on through your program structure, i.e.: doX(a); doY(a); ... you say what the value depends on through other values. ie.: b = doX(a); c = doX(b); — here b depends on a; and c depends on b, which depends on a.

Or, a more "visual" representation of this would be:

var a = Promise();

b = a.then(doX);
c = b.then(doY);

// (a)
//  |
//  v
// (b)
//  |
//  v
// (c)

This is how you sequence things when you replace dependency in time by dependency on data. You explicitly tell the program which data you need to run a certain computation. In the case above, computations are defined through the then method, which basically means: "When a is available, run doX".

Parallelism is, then, just the lack of sequential dependencies. That is, if I want to run a and b in parallel, and c when both are done, I just need to make b not depend on a:

var a = Promise();
var b = Promise();

var c = a.then(function(aValue) {
                 return b.then(function(bValue) {
                                 return doSomething(aValue, bValue) })})

// (a)       (b)     now A and B are computed in parallel,
//  |         |      since they depend on nothing
//  +----+----+
//       |
//       v
//      (c)          and C is computed in sequence, since
//                   it depends on both A and B

Now, on the particulars of Promises/A+, which is currently the most used implementation of promises in JavaScript. It gives you a .then method that takes two computations. One to run when the promise resolves successfully, and one to run when there's an error resolving the promise:

Promise.then(onSuccessFunction, onErrorFunction);

When calling the .then method, you immediately get a new promise, which depends on the value of either computation. Thus, this promise starts unresolved, and gets resolved whenever either the onSuccessFunction or onErrorFunction returns some data. You can then place a dependency on this new promise to perform more computations, like it was done on the parallel example above.

Where Promises/A+ differs of most promise implementations is that, if you return a Promise from the .then method, the promise that would be otherwise returned has an implicit dependency on this new promise value. Thus in:

var a = Promise();
var b = Promise();

var c = a.then(function(){ return b });

The action for the a.then will be ran immediately once a resolves, but c will still have an additional dependency on b, thus, if we were to add:

d = c.then(function(){ print("ok") });

ok would only be printed after a, b and c are successfully executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment