我可以将Promise传递给jQuery.when(),还是只传递Deferreds? [英] Can I pass Promises to jQuery.when(), or only Deferreds?

查看:90
本文介绍了我可以将Promise传递给jQuery.when(),还是只传递Deferreds?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery.when()的文档表示此函数采用Deferreds。但是,它稍后也会说:

The documentation for jQuery.when() says that this function takes Deferreds. However, it also says later on that:


如果将单个参数传递给jQuery.when()并且它不是Deferred或者承诺...

If a single argument is passed to jQuery.when() and it is not a Deferred or a Promise...

这似乎暗示它也可以接受Promises。但Promise不是Deferreds - 它们有Deferred方法的子集。我想你可以说Deferred是一个Promise,但是Promise不是延期。

which seems to imply that it can take Promises as well. But Promises are not Deferreds - they have a subset of the Deferred's methods. I guess you could say that a Deferred is a Promise, but a Promise is not a Deferred.

问题:


  1. $ .when()可以接受Promises还是Deferreds?这似乎适合我的测试。

  2. 文档中是否有错误?我认为应该说$ .when()接受Promises,而不仅仅是Deferreds。


推荐答案


我想你可以说Deferred是Promise,但Promise不是延期。

I guess you could say that a Deferred is a Promise, but a Promise is not a Deferred.

实际上它们是完全不同的接口,只有jQuery在promise API中混合到它们的延迟中。请参阅 JavaScript中的延迟,承诺和未来之间有什么区别?

Actually they're completely different interfaces, only jQuery mixed in the promise API into their deferreds. See What are the differences between Deferred, Promise and Future in JavaScript?


可以 $。when()接受Promises还是Deferreds?

Can $.when() take either Promises or Deferreds?

是的,它可以同时使用。但请注意,当您已经知道有承诺或延期时,在 $。上调用 $。时没有意义。

Yes, it can take both. But notice that when you already know to have a promise or a deferred, there is no point in calling $.when on it.


文档中是否有错误?

Is there a bug in the doc?

是的,显然。它甚至不是事实,它只将延迟和承诺视为异步值 - 而是它做了某种鸭子打字检查。你想看看实际实施情况

Yes, apparently. And it's not even the truth that it only treats deferreds and promises as asynchronous values - rather it does some kind of duck-typing check. You'll want to have a peek at the actual implementation:

if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) )

然后调用 承诺方法(或另一个承诺方法,或者实际上任何 .promise 方法) value并期望返回的对象具有相应的可链接方法来添加侦听器。

It then calls the promise method (or another promise method, or really any .promise method) on the value and expects the returned object to have the respective chainable methods to add listeners.

这篇关于我可以将Promise传递给jQuery.when(),还是只传递Deferreds?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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