jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery的固有问题$.Deferred(jQuery 1.x/2.x)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@ Domenic有一篇关于jQuery deferred对象的失败的非常详细的文章You’re missing the Point of Promises.在它中,Domenic强调了jQuery promises与其他包括 Q,when.js,RSVP.js和ES6承诺的一些失败。

我离开Domenic的文章感觉,jQuery承诺有一个固有的失败,从概念上。我试图把例子的概念。

我收集有两个关注的jQuery实现:

.then方法不可链接

换一种说法

promise.then(a).then(b)

当promise被满足时,jQuery将调用一个然后b。

因为.then在其他promise库中返回一个新的promise,它们的等价将是:

promise.then(a)
promise.then(b)

2.异常处理在jQuery中起泡。

一个问题似乎是异常处理,即:

try {
  promise.then(a)
} catch (E) {
}

Q中的等价将是:

try {
  promise.then(a).done()
} catch (E) {
   // .done() re-throws any exceptions from a
}

在jQuery中,当catch块失败时,异常抛出和冒泡。在其他承诺中,a中的任何异常都将被传递到.done或.catch或其他异步catch。如果没有promise的API调用捕获异常,它就消失了(因此Q最好的做法,例如使用.done释放任何未处理的异常)。

上述问题是否覆盖了jQuery实现promise的担忧,或者是我误解了还是错过了问题?

编辑这个问题涉及jQuery< 3.0;从jQuery 3.0 alpha jQuery是Promises / A兼容。

解决方法

更新:jQuery 3.0已经解决了下面列出的问题。它是真正的Promises / A兼容。

是的,jQuery promises有严重和固有的问题。

也就是说,自从写这篇文章以来,jQuery做出了重大的努力,以更多的Promises / Aplus投诉,他们现在有一个.then方法链。

因此,即使在jQuery返回的Promise()。然后(a).then(b)对于promise返回函数a和b将如预期工作,解开返回值继续前进之前。如图所示fiddle

function timeout(){
    var d = $.Deferred();
    setTimeout(function(){ d.resolve(); },1000);
    return d.promise();
}

timeout().then(function(){
   document.body.innerHTML = "First";
   return timeout();
}).then(function(){
   document.body.innerHTML += "<br />Second";
   return timeout();
}).then(function(){
   document.body.innerHTML += "<br />Third";
   return timeout();
});

然而,jQuery的两个巨大的问题是错误处理和意外的执行顺序。

错误处理

没有办法标记一个jQuery promise被拒绝为“处理”,即使你解决它,不像catch。这使得jQuery的拒绝本质上破碎,很难使用,没有像同步try / catch。

你能猜到这里的日志吗? (fiddle)

timeout().then(function(){
   throw new Error("Boo");
}).then(function(){
   console.log("Hello World");
},function(){
    console.log("In Error Handler");   
}).then(function(){
   console.log("This should have run");
}).fail(function(){
   console.log("But this does instead"); 
});

如果你猜到“uncaught错误:boo”你是正确的。 jQuery的承诺不是安全的。他们不会让你处理任何抛出的错误,不像Promises / Aplus承诺。拒绝安全怎么办? (fiddle)

timeout().then(function(){
   var d = $.Deferred(); d.reject();
   return d;
}).then(function(){
   console.log("Hello World");
},function(){
    console.log("In Error Handler");   
}).then(function(){
   console.log("This should have run");
}).fail(function(){
   console.log("But this does instead"); 
});

以下日志“在错误处理程序”“但这确实相反” – 没有办法处理一个jQuery promise拒绝。这不像你期望的流程:

try{
   throw new Error("Hello World");
} catch(E){
   console.log("In Error handler");
}
console.log("This should have run");

这是你使用Promises / A库,如Bluebird和Q,以及你期望的有用性的流程。这是巨大的,投掷安全是承诺的一个大卖点。这里是Bluebird acting correctly in this case

执行顺序

jQuery将立即执行传递的函数,而不是推迟它,如果底层的promise已经解决,因此代码将表现不同,取决于我们是否附加一个处理程序的拒绝已经解决的承诺。这实际上是releasing Zalgo,可以导致一些最痛苦的错误。这会创建一些最难调试的bug。

如果我们看下面的代码:(fiddle)

function timeout(){
    var d = $.Deferred();
    setTimeout(function(){ d.resolve(); },1000);
    return d.promise();
}
console.log("This");
var p = timeout();
p.then(function(){
   console.log("expected from an async api.");
});
console.log("is");

setTimeout(function(){
    console.log("He");
    p.then(function(){
        console.log("̟̺̜̙͉Z̤̲̙̙͎̥̝A͎̣͔̙͘L̥̻̗̳̻̳̳͢G͉̖̯͓̞̩̦O̹̹̺!̙͈͎̞̬ *");
    });
    console.log("Comes");
},2000);

我们可以观察到,这样的危险行为,setTimeout等待原始超时结束,所以jQuery切换其执行顺序,因为…谁喜欢确定性的API,不会导致堆栈溢出?这就是为什么Promises / A规范要求promise总是延迟到事件循环的下一次执行。

边注

值得一提的是,像Bluebird(和实验When)这样的更新和更强大的promise库不需要像Q这样的链末端的.done,因为它们自己找出了未处理的拒绝,它们也比jQuery promises或Q promises 。

大佬总结

以上是大佬教程为你收集整理的jQuery的固有问题$.Deferred(jQuery 1.x/2.x)全部内容,希望文章能够帮你解决jQuery的固有问题$.Deferred(jQuery 1.x/2.x)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。