-1

I have a promise that returns a function, the function does have its own error handling but sometimes for some reason this gets missed (need to explore this later).

I want to add a fallback that if the promise fails/null then return another function.

if (completedForm.isValid()) {
   return formDataQueue.push(formJson, this.company).then(function () {
      return self.trySync();
   });
}

Return self.trySync(): needs a error handler to assume it's not there as if it was commented out. My attempt does not seem to work.

if (completedForm.isValid()) {
   return formDataQueue.push(formJson, this.company).then(function () {
      //return self.trySync();
   }, function(error) {
      router.navigate('home');
   });
}
Bojan B
  • 2,091
  • 4
  • 18
  • 26
Tom Rudge
  • 3,188
  • 8
  • 52
  • 94
  • If `trySync` doesn't return a promise, you can test for that, but if it returns a promise that never resolves, it's going to be harder – adeneo Oct 14 '16 at 11:20
  • What exactly do you mean by "*assume it's not there as if it was commented out*". What would cause it to "not be there"? – Bergi Oct 14 '16 at 11:24

1 Answers1

1

Have a look at the difference between .then(…).catch(…) and .then(…, …).

To handle errors from trySync() (and only from there), you want to use

if (completedForm.isValid()) {
    return formDataQueue.push(formJson, this.company).then(function () {
        return self.trySync().catch(function(error) {
            router.navigate('home');
        });
    });
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375