For those of you who are working on applications that have to talk heavily
to the server I've created a library called Promises. Promises allows you
write arbitrarily complex requests
in a very sane way.

Get here: http://github.com/swannodette/Promises/tree/master

Short story, with promises you can write the following:

var get = function(rsrc)
{
  return new Request({
    url: 'data/'+rsrc+'.json'
  });
}.decorate(promise)

var add = function(a, b)
{
  return a + b;
}.decorate(promise)

var show = function show(value)
{
  console.log(value);
}.decorate(promise)

function example1()
{
  show(add(add(add(add(add(get('a'), get('b')), get('c')), get('d')),
get('e')), get('f'))); // -> abcdef
}

Yes this is what it looks like. 6 simultaneous requests, no callbacks, no
need to use Group,
no need to queue your requests. Promises allows you compose multiple
Requests as if
they were normal function calls.

You no longer have to write atrocities like the following:

function get(rsrc, callback)
{
  new Request({
    method: 'get',
    url: rsrc+'.json',
    onComplete: callback
  }).send();
}

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);
          });
        });
      });
    });
  });
});

Reply via email to