Something I would recommend, from having written heaps of asynchronous object based systems, with or without remote computers involved, is to treat all references to the promise object going away as an error, and avoid un-needed circular references.
This makes it easy to detect when you accidentally forget the object.
Also, having the object be callable is rather convenient, it can then be put as a normal callback in all the various function based API:s we already have.
From the MiniRPC module, used extensively in the mini codebase:
// This is an object representing a function to be called when // whatever task is being requested has been completed. // // In the RPC case the 'call_id' and 'par' are used to send the return // value, and function_name is for debug purposes. // class ReturnFromCall(protected int call_id, protected object par, protected string function_name) { protected void destroy() { // this should check the done function callback instead.. if( call_id ) { werror("Return was never called for "+function_name+"\n"); if( par ) this.error("Return was never called\n"); } }
protected string _sprintf( int flag, mapping opts ) { // It's rather nice to have a _sprintf for backtraces. But // without a function_name it's harder :) return sprintf("ReturnCall(from %O)", function_name ); }
protected void error(string err) { // .. this is sort of special in the RPC code .. call_id = 0; destruct(this); }
void `()(mixed ... x ) { // Wanted addition #2: Callable promises? call_id = 0; destruct(this); }
mixed try( function x ) //! Call @[x], if the call fails report an error message to //! the remote side. // // syntax of the day: // foo( foo->try() { code to run..; return res;}); // // It might be useful to have a functon that also returns the // value calculated { if( mixed err = catch{return x();}) { // code goes here to log etc.. this.error(err); } } }
Also, I personally belong to the "future/promises don't really add much" camp, but feel free to add the API. :)
(In my opinion it's just another way to complicate asynchronous programming)