Simply put, the more straightforward explanation is:
$q.reject("error")
generates a fresh promise object that is triggered by the $q.when().then()
.
This will not function as expected. When you use $q.reject("error")
, it yields an object with a then
function attached to it.
Refer to the Methods section in the documentation:
promiseB = promiseA.then(function(result) {
// success: take action and resolve promiseB
// with the original or a new result
return result;
}, function(reason) {
// error: try to handle the error and
// resolve promiseB with newPromiseOrValue,
// otherwise pass on the rejection to promiseB
if (canHandle(reason)) {
// manage the error and recover
return newPromiseOrValue;
}
return $q.reject(reason);
});
Observe how they utilize
then(function () { return $q.reject(reason); })
, which differs significantly from your initial approach.
Check out the updated example