In the code below, I am calling Pouch.sync(), to which I chain deleteLocal(). Pouch.sync() executes localDb.sync(), which does not return a promise, but I can attach a .on() event handler to handle errors.
I've tried playing around with $q.when and $q.reject, but I can't seem to prevent deleteLocal() from firing if the sync fails, and onError() never executes (either as an error callback or in the catch statement).
// Controller
function _onSyncClick() {
return Pouch.sync()
.then(deleteLocal, onError); // I do not want deleteLocal() to execute if Pouch.sync() fails
.catch(onError);
}
// Pouch service
function sync() {
return
localDb.sync(remoteDb)
.on('error', function (err) {
return $q.reject('Sync error.');
});
}