Why jQuery 3.0 Hides Errors and How to Catch Them with Promises
This article explains why errors thrown inside a jQuery 3.0 ready handler are silently ignored, compares the behavior with jQuery 2.x, and shows how to use the new Promise‑based $.ready() API with fail() to surface those errors in the console.
jQuery introduced a major change in version 3.0: the ready handler now returns a Promise. If you simply write
$(function(){ throw new Error('出错了'); $('#container').html('你好'); }), the error is swallowed and the page shows no content, while the console remains silent.
In contrast, using jQuery 2.1.3 the same code triggers a visible error in the console, making debugging easier.
Because jQuery 3.0 officially supports Promise , you can handle errors by chaining $.ready().then(...).fail(...). For example:
$.ready().then(function() {
throw new Error('出错了');
$('#container').html('你好');
}).fail(function(error) {
throw error;
});Now the console correctly displays the error message.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
