Hello Closure Users,
The Closure team is in the final stages of graduating the new asynchronous primitives (Result, chain, transform, combine) from labs to the core library and wanted to ask for any comments you might have on their syntax, naming or usage. These primitives are developed to offer a simpler way to do asynchronous communication and replace the currently used Deferred's.
Following are the current outstanding issues:
1) Syntax
Usage of wait :
var result = xhr.get('data.url');
goog.concurrent.wait.onSuccess(result, function(result) {
var value = result.getValue();
alert("Value: " value);
});
// There is also onError used to attach handlers when the result errors.
// result.getError() is used to retrieve the error value.
Usage of chain (a chain of results resolved in order : intended to be used for serial operations) :
var result1 = xhr.get('some.data.url');
var chainedResult = goog.concurrent.chain(result1, function(result) {
var secondUrl = result.getValue();
var result2 = xhr.get(secondUrl);
return result2;
});
// Use wait methods to wait on the chainedResult.
// chainedResult.getValue() returns the value of result2 on success.
// chainedResult resolves to an error when any result in the chain is an error.
Usage of combine (resolves when all results are resolved : intended to be used for parallel operations) :
var result1 = xhr.get('some.data');
var result2 = xhr.get('some.more.data');
var combinedResult = goog.concurrent.combine(result1, result2);
// Use wait methods to wait on the combinedResult which resolves when both result1 result2 are resolved.
// combinedResult errors when either is an error.
Usage of transform (for processing data of an operation before using it; strip prefixes, parse JSON, etc) :
var transformer = function(resultValue) { return resultValue * 2; }
var result = xhr.get('some.data');
var transformedResult = goog.concurrent.transform(result, transformer);
// The value of transformedResult is the value returned by the transformer
// The transformer is not called if the result is an error.
2) Namespace for the new primitives:
The current proposal is to move them from goog.labs.async to goog.concurrent (goog.concurrent.Result, goog.concurrent.wait, etc) since goog.async already has (unrelated) primitives for delay, animation, throttle, and the like.
3) Naming of the object:
Currently the objects are called Results. Other proposed names are : Promises, Futures.
Please let us know if you have any comments, questions, or concerns.
Thanks,
Vipul