JQuery对象承诺的目的是什么? [英] What is the purpose of the JQuery object promise?

查看:75
本文介绍了JQuery对象承诺的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年来,我一直在处理一些兼职问题,其中一些DOM元素使用jquery html 函数在函数调用后不能立即访问(行为有点像执行函数,而不等待文档就绪事件)。尽管 .html()假设是同步的,但这个回答一个>(也是这个一个)建议使用诺言 html 函数:

  $('#divId')。html < h1> hello< / h1>)。promise()。done(function(){
// Callback
});

一个承诺在这样的背景下可能是有道理的,但是我对这个承诺有什么好感的是它也会返回每个jquery的对象:

  $('#divId')。promise()。done(function(){
//也会被称为...
});

由于我在 html的文档中找不到任何关于它的信息函数,我想知道这个承诺的真正目的是什么,如果它在这种情况下使用正确。

解决方案这两种方法是不相关的。人们经常暗示这是因为有人发现他们可以使用 .promise()。done()来让他们的代码具有竞争条件。它工作出于同样的原因,在 setTimeout(fn,0)中包装相同的代码会使其工作;它会将其推迟到回调队列中,稍后再运行,可能在浏览器执行渲染或其他异步回调完成后。



。 html 是同步的,没有回调,并且不需要回调。但是,浏览器的渲染器是异步的,所以它不会在调用堆栈清除之前渲染更改。使用.promise会将回调推回到回调队列中,从而在渲染后运行代码来解决竞争条件或异步逻辑漏洞。



在jquery集合上使用的.promise()返回一个承诺,一旦所有当前正在运行的jquery动画完成,这个promise就会解析。如果当前没有正在运行的动画,承诺将立即解决,并且回调将被推送到回调队列,以便在堆栈清除后被调用。



比绷带。我建议不要使用它,而是修复任何异步逻辑缺陷导致它成为解决方案。


For a couple of years, I was dealing with a part time problem where some DOM elements loaded with jquery html function were not accessible immediately after the function call (The behaviour was a bit like executing a function without waiting for the document ready event). Even though .html() is suppose to be synchronous, this SO answer (and also this one) suggests using the promise of the html function:

$('#divId').html("<h1>hello</h1>").promise().done(function(){
    //Callback
});

A promise could make sense in such context but what intrigues me about this promise is that it will also be returned with every jquery's object:

$('#divId').promise().done(function(){
    //Will also be called...
});

Since I couldn't find anything about it in the documentation of the html function, I was wondering what was the real purpose of this promise and if it is used correctly in this context.

解决方案

The two methods are not related. The reason people often suggest this is because someone found out that they could use .promise().done() to make their code that has a race condition work. It worked for the same reason wrapping the same code in setTimeout(fn, 0) would make it work; it pushes it off to the callback queue to be ran later, likely after the browser performs a render or after some other async callback completes.

.html is synchronous, does not have a callback, and does not need a callback. However, the browser's renderer is asynchronous, so it won't render the change until after the callstack is clear. Using .promise pushes the callback off to the callback queue, thus running the code after a render which resolves the race condition or async logic flaw.

.promise() used on a jquery collection returns a promise that will resolve once all currently running jquery animations are complete. If there are no currently running animations, the promise will resolve immediately and the callback will be pushed to the callback queue to be called once the stack is clear.

It's nothing more than a bandaid. I'd suggest not using it, and instead fixing whatever async logic flaw is causing it to be the solution.

这篇关于JQuery对象承诺的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆