Hi guys!

I started experimenting with the new Concurrent.Promise/Concurrent.Future classes and implementing a HTTP client module using the promise/future stuff. The reason for this is that the async stuff in Protocols.HTTP.Query is far from intuitive. With promises/futures I think you get a super clean API that is easy to use. 

So I started experimenting with this and would like to have your thoughts on the matter. Right now I put it as Protocols.HTTP.Promise but any suggestions on what to call it and where to place it would be appreciated.

Here’s some examples of how it would be used at the moment.

// Fetch a single URL

import Protocols.HTTP;

int main()
{
  string url1 = ”http://pike.lysator.liu.se”;

  Concurrent.Future q1 = Promise.get_url(url1);
  
  q1->on_success(lambda (Protocols.HTTP.Query ok) {
    werror(”Successfully got %O\n”, ok->host);
  })->on_failure(lambda (Protocols.HTTP.Query fail) {
    werror(”%O failed!\n”, fail->host);
  });

  return -1;
}


// Fetch multiple URLs and call on_success in parallell for each request.

import Protocols.HTTP;

int main()
{
  string url1 = ”http://pike.lysator.liu.se”;
  string url2 = ”http://roxen.com”;

  Concurrent.Future q1 = Promise.get_url(url1);
  Concurrent.Future q2 = Promise.get_url(url2);

  array(Concurrent.Future) all = ({ q1, q2 });
  
  all->on_success(lambda (Protocols.HTTP.Query ok) {
    werror(”Successfully got %O\n”, ok->host);
  })->on_failure(lambda (Protocols.HTTP.Query fail) {
    werror(”%O failed!\n”, fail->host);
  });

  return -1;
}


// Fetch multiple URLs and call on_success when all has succeeded. 
// on_failure will be called immediately if one request fails.

import Protocols.HTTP;

int main()
{
  string url1 = ”http://pike.lysator.liu.se”;
  string url2 = ”http://roxen.com”;

  Concurrent.Future q1    = Promise.get_url(url1);
  Concurrent.Future q2    = Promise.get_url(url2);
  Concurrent.Future batch = Concurrent.results(({ q1, q2 }));
  
  all->on_success(lambda (array(Protocols.HTTP.Query) ok) {
    werror(”Successfully got %O\n”, ok->host);
  })->on_failure(lambda (Protocols.HTTP.Query fail) {
    werror(”%O failed!\n”, fail->host);
  });

  return -1;
}

So, any thoughts on this?

Regards
-----------------------------
Pontus Östlund
Developer • Roxen AB
+46 70-662 81 69

www.roxen.com | twitter.com/roxen