I've implemented an easily portable library on top of MooTools for writing arbitrarily nested AJAX requests without requiring callbacks of any kind. Long story short, Promises can be composed, AJAX requests cannot.
Instead of:
var result = '';
get('a', function(response) {
result += JSON.decode(response).data;
get('b', function(response) {
result += JSON.decode(response).data;
get('c', function(response) {
result += JSON.decode(response).data;
get('d', function(response) {
result += JSON.decode(response).data;
get('e', function(response) {
result += JSON.decode(response).data;
get('f', function(response) {
result += JSON.decode(response).data;
console.log(result);
});
});
});
});
});
});
You can write: function example()
{
var resources = ['a', 'b', 'c', 'd', 'e', 'f'];
var result = "";
while(resource = resources.shift())
{
result = add(result, get(resource));
}
show(result); // -> abcdef
}
This is completely asynchronous. Notice that there are no callbacks. You write code with Promises as you would write regular JavaScript instead of the atrocity that is the AJAX request ;)