[dojo-contributors] Promises

Kris Zyp kzyp at dojotoolkit.org
Wed Mar 3 17:10:28 EST 2010


I put together a backwards compatible variant of this implementation.
The then() method continues to create new promises for the result of
callback execution, safely avoiding side-effects/self-mutating deferred,
while addCallback/addErrback/addBoth/addCallbacks preserves existing
self-mutating behavior. This should make this version fully backwards
compatible. This version weighs in at 2088 bytes (compressed), still
about 140 bytes less than the existing implementation. Would it be
conceivable to use this as a replacement for Deferred.js in Dojo 1.5
since we should be able to do so without breaking anything? We would
then be in position to deprecate addCallback and friends and have a
smooth transition to the more compact, safer version in the future
without abrupt API change. I could check my version in as a branch or in
a ticket for further review and consideration.
Thanks,
Kris

On 2/1/2010 11:40 AM, Kris Zyp wrote:
> I checked in an implementation of my proposed promise system for Dojo
> 2.0 to play with at dojoc/sandbox/promise/Deferred.js. Here are the size
> stats (all compressed):
> Size of current Deferred.js impl: 2,225 bytes
> Size of my Deferred.js: 1,775 bytes
> Breakdown of parts (any of which could probably be eliminated to make it
> even smaller, although I think they are all worth their size):
> Progress events: 100 bytes
> dojo.when: 120 bytes
> addCallback, addErrback, addBoth, and addCallbacks for back-compat: 220
> bytes
> Throwing errors if nothing in chain registers to handle it: about 150 bytes
> Cancellation: 120 bytes
> Maintaining the queue: about 30 bytes
> Ridiculously large namespace: 120 bytes
> The rest is core promise functionality
>
> Some things you can do with this:
> dojo.require("dojoc.sandbox.promise.Deferred");
>
> var deferred2 = dojoc.sandbox.promise.Deferred();
> var plus4 = deferred2.then(function(value){ return value + 4; });
> plus4.then(function(value){
>   console.log(value);
> });
> deferred2.resolve(2);
>
> function print(value){
>   dojo.when(value, function(value){
>     console.log(value)
>   });
> }
>
> print(5);
>
> print(deferred2);
>
> var deferred3 = dojoc.sandbox.promise.Deferred();
> print(deferred3);
> deferred3.resolve(3);
>
> var deferredErrorNoHandler = dojoc.sandbox.promise.Deferred();
>
> deferredErrorNoHandler.reject(new Error("uncaught"));
>
> var deferredError = dojoc.sandbox.promise.Deferred();
>
> deferredError.then(function(){}, function(){ console.log("caught error")});
> deferredError.reject(new Error("will be caught"));
>
> deferred3.addCallback(function(value){
>   console.log("addCallback still works " + value);
> });
>
> deferredError.addErrback(function(error){
>   console.log("and addErrback ", error);
> });
>
> var deferredCancel = dojoc.sandbox.promise.Deferred(function(){
>   console.log("cancelled")
> });
>
> deferredCancel.cancel();
>
>
> deferredProgress = dojoc.sandbox.promise.Deferred();
>
> deferredProgress.then(function(value){
>     console.log("all done", value);
>   },
>   function(){
>       console.log("an error");
>   },
>   function(partial){
>       console.log("in progress", partial);
>   });
>
> deferredProgress.progress("The prom");
> deferredProgress.progress("The promise is");
> deferredProgress.resolve("The promise is now complete");
>
> Thanks,
> Kris
>
> On 1/26/2010 1:07 PM, James Burke wrote:
>   
>> On Tue, Jan 26, 2010 at 8:29 AM, Kris Zyp <kzyp at dojotoolkit.org> wrote:
>>   
>>     
>>> Perhaps it would be useful for us each to come up with an impl, so we
>>> can actually measure the code size for each approach. Then we could have
>>> working code and samples to test as well.
>>>     
>>>       
>> +1
>>
>> James
>> _______________________________________________
>> dojo-contributors mailing list
>> dojo-contributors at mail.dojotoolkit.org
>> http://mail.dojotoolkit.org/mailman/listinfo/dojo-contributors
>>
>>   
>>     
> _______________________________________________
> dojo-contributors mailing list
> dojo-contributors at mail.dojotoolkit.org
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-contributors
>
>   


More information about the dojo-contributors mailing list