-1

const errorTest = () => {

  const result = $.get("http://data.fixer.io/api/latest?access_key=9790286e305d82fbde77cc1948cf847c&format=1");

  result
    .then(val => console.log(val.rates))
    .catch(err => console.error(err))


}

errorTest()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

.then() works, but catch() doesn't. It keeps showing an error: result.then(...).catch is not a function at errorTest.

Why? $.get() is supposed to return a promise, and assign it to result. That's why .then() works, but why is catch() not working?

It works if I use fetch() instead.

 const result = fetch("http://data.fixer.io/api/latest?access_key=9790286e305d82fbde77cc1948cf847c&format=1");

So, why is catch() not working with ajax requests then?

ADDITIONAL QUESTION

const errorTest = (a, b) => { 

  const result = $.get("http://dataa.fixer.io/api/latest?access_key=9790286e305d82fbde77cc1948cf847c&format=1");

  result
  .then(val => console.log(val.rates))
  //.catch(err => console.error("INSIDE ERROR" + err))

}


try { 
 errorTest()
}
catch(err) { 
  console.log("OUTSIDE ERROR!" + err)
}

The link is intentionally incorrect to get an error. If I uncomment the catch() inside the function, that inside catch() will capture the error. If I comment out, or just remove the inside catch(), I would expect the outside catch() to get the error, but it's not. Why?

happy_story
  • 1
  • 1
  • 7
  • 17
  • 2
    it;s working fine. i tested with jquery version 3.6.0 – Gulam Hussain Jun 23 '21 at 12:12
  • OP added a link to jQuery 3.3.1, but it works just fine with that as well. Tested. – Amunium Jun 23 '21 at 12:14
  • I added the jquery – mplungjan Jun 23 '21 at 12:14
  • Rather than just looking at `val.rates` , you might want to check out the full return -> `{ "success": false, "error": { "code": 105, "type": "https_access_restricted", "info": "Access Restricted - Your current Subscription Plan does not support HTTPS Encryption." } }` – Keith Jun 23 '21 at 12:16
  • `result` in your first snippet isn't a promise, it's a [jqXHR](https://api.jquery.com/Types/#jqXHR) object. From jQuery 1.5 it has the standard methods you can expect from the Promise API such as `catch`, so maybe check your jQuery version – Nick Parsons Jun 23 '21 at 12:19
  • Why do I keep getting the error then? I didn't say `then()` doesn't work, I said `catch()` doesn't work. – happy_story Jun 23 '21 at 12:24
  • Your maybe running a very old version of JQuery. – Keith Jun 23 '21 at 12:26
  • Where can I get the new jQuery version? I can't find the link in their website? Can somebody just please paste the link. – happy_story Jun 23 '21 at 12:27
  • https://jquery.com/ There is a big button that says `Download JQuery`.. – Keith Jun 23 '21 at 12:31
  • Yep, it works now. I used the latest jQuery, and there's no problem anymore. – happy_story Jun 23 '21 at 12:31
  • @Keith I don't wanna download. I just wanted a link to the CDN, but I got it myself. Thanks. – happy_story Jun 23 '21 at 12:32
  • @ihatefacebook No problem, but that link also shows you CDN's too. It's just a bit further down, it then takes you to -> https://code.jquery.com/ with all the CDN links.. – Keith Jun 23 '21 at 12:36
  • @Keith A bit unrelated question but, why when I use `then()` inside the function block, and then use `try .. catch` outside the function, why is the error not thrown at the outside `catch()`, but instead, if there is no `catch()` inside the block, it's just not shown at all? – happy_story Jun 23 '21 at 13:09
  • @ihatefacebook Not really sure what your asking, I wonder if it's best to start another question showing example code. – Keith Jun 23 '21 at 13:13
  • @Keith I edited my original question to include the my question. Please take a took at it. – happy_story Jun 23 '21 at 13:36
  • Without using `async / await` a normal try catch is not going to work. More info -> https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Keith Jun 23 '21 at 13:42
  • @Keith So, I'm trying to create a case in which I am using two `catch()`, one inside, and one outside a function in order to demonstrate that the closest one will be used. Is such case even possible? If I use `async/await` then I can't use `catch()` inside, right? - because `await` will just return the resolved promise. – happy_story Jun 23 '21 at 13:46

1 Answers1

1

tl;dr

Regarding your problem, i think it just doesn't throw an exception, thats why .then() works.

However, instead of using the callback-based promise syntax, i suggest using async/await. It's a much cleaner way.

const errorTest = async () => { 

  try {
    const result = await $.get("http://data.fixer.io/api/latest?access_key=ACCESS_TOKEN&format=1");
    console.log(result.rates);
  } catch(e) {
    console.err(e);
  }

 
}

errorTest()
  • The issue was with the jQuery version. Apparently I wasn't using the latest version. I just tried it with the latest 3.6 version, and it works now. – happy_story Jun 23 '21 at 12:33
  • A bit unrelated question but, why when I use `then()` inside the function block, and then use `try .. catch` outside the function, why is the error not thrown at the outside `catch()`, but instead, if there is no `catch()` inside the block, it's just not shown at all? – happy_story Jun 23 '21 at 12:55