Can somebody tell me if there is a difference between using an error callback vs. a catch
function, when using $q.promise
please?
E.g. are the two snippets of code functionally equivalent?
function doSomething0() {
var deferred = $q.defer();
...
return deferred.promise;
}
doSomething0()
.then(doSomething1)
.then(doSomething2)
.then(doSomething3)
.catch(function (err) {
// do something with `err`
});
vs.
function doSomething0() {
var deferred = $q.defer();
...
return deferred.promise;
}
function errorHandler(err) {
// do something with `err`
}
doSomething0()
.then(doSomething1, errorHandler)
.then(doSomething2, errorHandler)
.then(doSomething3, errorHandler);
If so, why use the second one? It looks far uglier and leads to more code duplication in my opinion?
errorHandler
will be called three times only ifdoSomething3
fails. But ifdoSomething2
fails, it will be called two times and ifdoSomething1
fails, it will be called once. – Onepiece