Chained is an experiment and a prototype library for Javascript. It is meant to show a concept and to provide some limited but useful functionality. All the examples below are written in Coffeescript (and I guess that this makes me a hippie, right?).

Chained allows to chain functions explicitly — both those that return promises and those who don't — without using then-based constructs.

Here, we have chained together promise-based functions (like get from jQuery) and normal functions with additional parameters (like map and filter from underscore). All function invocations receive an implicit argument that is the value computed by the previous link in the chain.

The function defined in getUser implements a processing pipeline (à la streaming). It starts with the link to be retrieved by specifying it with _():

_("https://npmjs.org/~#{user}")

this value is then passed to get() of jQuery. Chained knows that jQuery has a get method since we introduced its scope with

using(jQuery)

at the beginning[^1].

The result of get() — i.e. the webpage — is sent to extractLinks (from linklib) when the associated promise is resolved.
The rest of the computation proceeds as you can intuitively imagine.

If an exception is thrown in any of the links of the processing chain then the following links are not executed, just as you would expect from promises.

We can add additional arguments (besides the implicit one) to method invocations; for example the filter function (from underscore) is invoked as:

…
.filter( -> /package/.test(arguments[0]) )
…

where the lambda function is passed effectively as the second parameter to underscore.filter (the first being the implicit parameter).

So we nailed a few things here:

We can chain promise-based functions and normal functions without distinction using the natural syntax of Javascript.

We can use directly their names instead of using then.

We can chain any method from an arbitrary number of modules, provided that the module is bound to Chained with the using clause.

We don't modify dynamically the module that are imported. We don't change any prototype.

The chain stops executing when one of the links throws an exception or a promise is rejected.

We are not using any coffee-script mambo-jumbo for chaining; we can write with the same expressiveness in pure Javascript.

We are exploiting ECMA6/harmony implementation of introspection.
You need an ECMA6/harmony implementation, either in node (node --harmony) or in your browser (Firefox is ok at the moment) to make it work.

Yes, check out this example Domain Specific Language for form validation directly built with Chained. Warning: it needs a recent build of Firefox. In principle also a harmony-enabled Chrome should work, but I did not try it.

The idea for Chained came while tinkering with domain specific languages. DSLs provide the developer with the vocabulary of a problem domain to express a programmatic solution that could be understood by a problem domain expert[^2].

I will not cover all the general concepts of DSLs here. For an in depth overview, I'd suggest to look at these resources:

Internal DSLs are built using the syntactic and semantic features of another language; they are used in the context of a broader application for which they address a narrow but important problem.

My quest was to find a way to implement a DSL in Javascript. Let's see what features are needed by a general purpose language like Javascript to support the definition of internal DSLs:

Support for named arguments (or hashes) in function invocation (aka Smart API), e.g.:

move(the: pen, on: theTable)

Support for a property/method missing exception to implement method chaining:

take('spaghetti').and().cookIt(at: 100C)

here and is a non existing method of the return value V of take('spaghetti'). The missing exception handler will return the same value V so that cookIt could be called on it.

(optional, but highly desirable)

Reduced boilerplate code.

Synthetic expression of closures (w/ implicit arguments).

Limited presence of parenthesis {} and ():

withObject chair, ->
move it, in: theLivingRoom

At some point, it became clear to me that DSLs' method chaining can be used to implicitly structure promise based chains. In fact, you could build a promise-chain by handling a sequence of method missing exceptions.

As a byproduct, it is straightforward to use the same technique to chain normal functions, without any exogenous construct like then.

Javascript and its relatives (such as Coffee and LiveScript) have almost all the characteristics to build a sophisticated DSL. The only problem is that they lack of a method missing exception; at least until Harmony came out.

Dylan Barrell already demonstrated that with the new reflection API, it is possible to manage missing methods. His technique exploits the concept of Proxies. Think about a Proxy like a wrapper around your original object that is able to intercept calls to methods that are not defined. Chained is based on Dylan's work.