#1841 Asynchronous callback patterns

dobesv Fri 16 Mar 2012

Trying to decide how to handle errors and callbacks with the asynchronous API.

In node.js they pass two parameters - the error and the result. You do if(err) { ... } else { ... }.

In GWT and Java's async system you provide a class that implements onSuccess() and onFailure() (usually in an anonymous inner class).

In jQuery you supply separate callbacks for success, failure, and/or "done" which covers both.

Also to consider is having a callback that takes an object which contains fields for error or no error (instead of passing two parameters, pass an object).

I'm currently looking at passing the callback a function which either returns the callback result or throws the error exception. So you use it like:

socket.read
{
  try
  {
     buf := it() // it() throws an exception if there was an error
     // .. do something with the data
  }
  catch(Err err)
  {
     // .. handle the error somehow
  }
}

Anyone have any thoughts on the matter?

brian Fri 16 Mar 2012

That is pretty much like exactly how I made Future.get work - it either returns the value or throws an exception. I like the idea of a single object that manages the entire result (ok or error).

Login or Signup to reply.