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?