Per,

Your hack won't work with my case as it would mark the deferred as
`chained` so consecutive addCallback/addErrback won't work. Considering
Bob's suggestion, I modified my patch to ensure no further
callback/errbacks can be registered.

Regards
--
Amit

Bob Ippolito wrote:
> Finalizing a Deferred should ensure that no further callback/errbacks
> are registered and it should attach a default error handler (success
> should be no-op). The most common problem I've seen with Deferreds is
> that an error occurs but nobody attached an error handler that far
> down the stack. In Python they work around this by having a finalizer
> so that you see the error when the object gets GC'ed, but that's not
> really possible in JS since you don't have finalizers or weak
> references.
>
> On Tue, Feb 10, 2009 at 8:04 AM, Per Cederberg <[email protected]> wrote:
>   
>> I think this is a good idea. I needed something similar too, so I
>> ended up writing an ugly hack that worked most of the time...
>>
>>    d.addBoth(function (res) {d.addBoth(finalFunc); return res; });
>>
>> It adds new callback once the first deferred result drops in,
>> hopefully after all the other callbacks have been added...
>>
>> But a more formally correct solution would probably be a good idea.
>>
>> Cheers,
>>
>> /Per
>>
>> On Tue, Feb 10, 2009 at 2:30 PM, Amit Mendapara
>> <[email protected]> wrote:
>>     
>>> Hi Per,
>>>
>>> I have just started again improving the MochiKit Extensions. While
>>> creating tests for the Ajax module, I found one problem (not bug, but
>>> specific to the feature I'm trying to implement). I registered a
>>> callback which should be fired at the end of all registered callbacks.
>>>
>>> I achieved by modifying Async.js with a new method `setFinal` (not
>>> addFinal as there should be only one finalizer)  which gets fired when
>>> `chain.length == 0`. It's simple. I would we happy if you add one such
>>> function in MochiKit.Async.Defered...
>>>
>>> Regards
>>> --
>>> Amit
>>>
>>>       

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---

Index: MochiKit/Async.js
===================================================================
--- MochiKit/Async.js	(revision 1525)
+++ MochiKit/Async.js	(working copy)
@@ -20,6 +20,7 @@
     this.canceller = canceller;
     this.silentlyCancelled = false;
     this.chained = false;
+    this.finalized = false;
 };
 
 MochiKit.Async.Deferred.prototype = {
@@ -129,6 +130,9 @@
         if (this.chained) {
             throw new Error("Chained Deferreds can not be re-used");
         }
+        if (this.finalized) {
+            throw new Error("Finalized Deferreds can not be re-used");
+        }
         this.chain.push([cb, eb]);
         if (this.fired >= 0) {
             this._fire();
@@ -136,6 +140,11 @@
         return this;
     },
 
+    setFinal: function(cb) {
+        this._final = cb;
+        return this;
+    },
+
     _fire: function () {
         /***
 
@@ -184,6 +193,10 @@
             res.addBoth(cb);
             res.chained = true;
         }
+        if (this.chain.length == 0 && this._final) {
+            this.finalized = true;
+            this._final();
+        }
     }
 };
 

Reply via email to