Deferred resolution still in capture test

微信扫一扫,分享到朋友圈

Deferred resolution still in capture test

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?

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 fail
code, 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.

微信扫一扫,分享到朋友圈

Deferred resolution still in capture test

Woman accidentally informs the U.S. that she's a terrorist via online travel form

上一篇

Index indefinite in table, but it is there

下一篇

你也可能喜欢

Deferred resolution still in capture test

长按储存图像,分享给朋友