0

I'm new to ajax and javascript, and am a little confused as to why my error is not being caught when my ajax call is asynchronous.

I looked through this question already (Catch statement does not catch thrown error), but I am already calling my function which catches my error inside my success block in Ajax.

My code looks something like this:

var Codecheck = { 
   SundayCheck : function() {
      this.check = function (date, boolean) {
         if (boolean) return; //skip check
         else{
              ...logic to check if sunday
             if(!Sunday) 
                throw new Error
          }
       }
   }


 checkSunday : function(title) {

        $.ajax({
        url : url,
        type : 'GET',
        dataType: 'json',
        contentType : 'application/json;charset=UTF-8',
        async: false,
        success : function(response) {
            if (response.content == 'checkSunday') {
                new Codecheck.SundayCheck().check(date, true);
            }
            else {
                new Codecheck.SundayCheck().check(date, false);
            }
        },
        error : function(response) {
            document.write(response.responseText);
        }
    });

This works fine, and my error is caught. However, once I remove the async: false, the error is no longer caught. I'm calling my check method, and therefore catching my error inside of the success block in ajax so I'm not really sure what's going on.

The only thing I can think of is whatever calls my checkSunday function, the code that is suppose to run afterwards will run if it's async call whereas if it's a sync call then the code will wait.

However, this still doesn't explain how the error handling that should be caught in the method that's only called upon a successful ajax response is not being called regardless of sync or async.

Kevin
  • 3,209
  • 9
  • 39
  • 53
  • WHERE is the error bring thrown? What is the error? – epascarello Jan 18 '18 at 01:43
  • Sorry, I've updated my code block to make it more clear. The error is thrown in the method that ajax calls inside the success block. – Kevin Jan 18 '18 at 01:45
  • by the way, you define `CheckSunday` and `checkSunday` in `Codecheck` but call `Codecheck.checksunday` or `Checkers.checksunday` - so there's no real indication of what code your success function would be calling – Jaromanda X Jan 18 '18 at 02:02
  • I'm pretty sure the `error` parameter is supposed to reserved for a failed http response, as opposed to an error throw. If you run it async it simulates a failure when you use `throw new Error` – Andrew Jan 18 '18 at 02:35
  • @JaromandaX Sorry, I was quickly just making a prototype code for this question. I updated my question, but in the real code it's not this ambiguous / I don't have a typo in my real code. – Kevin Jan 18 '18 at 18:26
  • @Andrew yes in this case, my error parameter is to document the failed http response. It's in the function I call in the success block, SundayCheck.check, that actually throws the javascript error which isn't being caught if it's run async. So the scenario is my ajax call makes a call to get a boolean indicating whether we should do a sunday check. If true, then we do a sunday check and if the date is not a sunday, throw a javascript error. – Kevin Jan 18 '18 at 18:30

1 Answers1

0

Ah I got it. I didn't write this code originally so I wasn't aware of everything happening, but the reason is the try/catch block that catches these javascript errors is outside of my ajax call. My ajax call just throws the error.

Thus, if this happens asynchronously, then the try catch will execute before my error is thrown.

Kevin
  • 3,209
  • 9
  • 39
  • 53