I have the following code snippet:
var myFunc = function(id, obj, backupObj) { return update(obj) // this returns a promise .fail(function(err){ // if update is rejected restore(id, backupObj); // i wish to run this // and return the error that 'update(obj)' threw and propagate it return Q.reject(err); // should it be thrown like this??? }); }
Is this the correct way to handle update() failure and returns an error, so when myFunc() is called from anywhere, can it be propagated and finally handled? For example, executed inside a chain:
var foo = function(id, obj) { var backupObj = {}; return checkIfValidObj(obj) .then(function(_backupObj) { backupObj = _backupObj; return doSomethingElse(id, obj); }) .then(function() {
And then, as the caller of foo , I would like to handle some errors and catch the error that update(obj).fail() can return:
foo() .then(function() { }) .catch(function(err){
source share