The primary function of the .done
method in Q is to properly handle errors without suppressing them.
In a scenario where you have a rejected promise chain in Q, using .then
all the way through results in a silent failure. Consider the following code snippet:
Q().then(function(){
var val = JSON.prase(data);
someEffectWith(val);
});
This code silently fails due to a typo and the inability to determine when the chain has ended. This emphasizes the importance of utilizing .done
to signal to the library:
Q().done(function(){
var val = JSON.prase(data);
someEffectWith(val);
});
Alternatively, you can also do:
Q().then(function(){
var val = JSON.prase(data);
someEffectWith(val);
}).done();
Both of these approaches will trigger a prominent red warning in your console, alerting you to the error. The .done
method accepts the same arguments as .then
, but it does not return a promise; instead, it returns undefined
, indicating that further chaining is not possible (chain termination).
It's important to note that certain promise libraries, including native promises in Firefox, automatically handle this error logging, negating the need for explicit use of .done
.