Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .then does not throw errors

using $.get promise (jquery 3.3.1) won't display any error, which makes debugging impossible

$.get('/').then(function() {
    console.log(undef)
})

fetch('/').then(function() {
    console.log(undef)
})

The first example is silent, no error thrown

The second example throws as expected "Uncaught (in promise) ReferenceError: undef is not defined"

Is it a jquery bug or am I using it in a wrong way? How'd you suggest using fetch instead? I'm concerned about browser comatibility

like image 646
orfaust Avatar asked Dec 17 '25 19:12

orfaust


2 Answers

You can add the fail function in the chain:

$.get('/').then(function() {
    console.log(undef);
})
.fail(function(){
    console.log("Error");
})

Reference

This is by design. then() only executes when a request completes successfully.

If you want to know when a request has failed add a fail() handler:

$.get('/').then(function() {
  console.log(undef);
}).fail(function() {
  // something went wrong...
});
like image 20
Rory McCrossan Avatar answered Dec 20 '25 07:12

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!