ServerJS/Promises
Promises
Promises provide a well-defined interface for interacting with an object that represents the result of an action that is performed asynchronously, and may or may not be finished at any point in time. By utilizing a standard interface, different components can return promises for asynchronous actions and consumers can utilize the promises in a predictable manner. Promises can also provide the fundamental entity to be leveraged for more syntactically convenient language-level extensions that assist with asynchronicity.
Prior Art
- Dojo's Deferred A form of promises that was inherited from MochiKit and Twisted.
- ref_send. A promise manager.
Proposed API
the module 'promise' would return an export 'Promise' with a prototype with the following functions available:
- then(callback, errorHandler, progressHandler)
- Add separate callback, errorHandler, and progressHandler to the end of the callback sequence. Non-function values are ignored. This should return a new promise that is fulfilled when the given callback is finished. The value returned from the callback is the fulfillment value for the returned promise.
- wait()
- This will block the execution of the current function while waiting for the Promise to be fulfilled, *if* the implementation and this Promise support this operation. If supported, the function will return the result of the resolved Promise when it is fulfilled. If the Promise is resolved with an error, than this function will throw theprovided error object. If the concurrency model of the implementation or the Promise object does not support blocking, the "wait" property should be a null or undefined. This API is not intended to imply that implementations that provide a "wait" function are superior.
the module 'promise' would return an export 'InteractivePromise' with a prototype with the following functions available. The prototype object's Proto should point to the prototype of Promise.
- get(propertyName)
- Requests the given property from the target of this promise. Returns a promise to provide the value of the stated property from this promise's target.
- call(functionName, arg1, arg2, ...)
- Request to call the given method/function on the target of this promise. Returns a promise to provide the return value of the requested function call.
Related Discussions
http://groups.google.com/group/serverjs/browse_thread/thread/e93f73ef97e88439/9cdb490abb8edfa6