I’m using a standalone implementation (git repo) of the jQuery Deferred. Keeping the question simple, if I make a var status = _.Deferred() and return status.promise() from within any function, do I then have to add a try catch for all steps within that function to reject the deferred on error?
I’m using a standalone implementation ( git repo ) of the jQuery Deferred. Keeping
Share
In javascript, try/catch isn’t a way of life as in say Java. The reason is that javascript is fairly rich in ways to avoid predictable types of error.
It’s not strictly necessary to reject a Deferred. Typically you will do so if you need to execute some
failcode, or if you need to positively prevent some later event resolving the Deferred.It is tempting to think that unresolved/unrejected Deferreds hang for ever. This is not necessarily the case. A Deferred becomes available for garbage collection at such time that no reference to it exists in any scope, either directly or via its promise.
It is also tempting to think that resolved/rejected Deferreds are automatically garbage collected. This is also untrue. If a reference to a Deferred still exists, in any scope, then it will still be present in memory, and can still be useful given that watcher methods (eg done/fail/always/then) may be invoked later (and may fire immediately).
In short, with regard to GC, Deferreds are just like any other js object, though the code structures in which they are typically used make it difficult to spot when they might become available for GC.