本文介绍了在jQuery 1.8中使用pipe()和then()文档与现实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我一直在探索jQuery Deferred / Promise API for a a我非常困惑 pipe()和然后()之间的差异哲学和jQuery文档。我发现从jQuery 1.8开始,pipe()只是then()的别名。I've been exploring the jQuery Deferred/Promise API for a bit and I'm very confused about the differences between pipe() and then() philosophically and in the jQuery documentation. I've found that pipe() is just an alias for then() as of jQuery 1.8.来自jQuery源:// Keep pipe for back-compatpromise.pipe = promise.then;然而 pipe()的文档完全不同和然后()因为他们认为有完全不同的用途。Yet the documentation is completely different for pipe() and then() as they supposedly have entirely different uses. 的描述then(): pipe()的描述:我明白了从历史上看,他们的行为略有不同,但在整个管道文档或当时的文档,它甚至没有说这两个函数现在完全相同。 I understand that historically they had slightly different behavior, but in the entirety of the documentation for pipe or the documentation for then, it doesn't even say that these two functions do the exact same thing now.所以,这是我的两部分问题:So, here's my two part question: 为什么 pipe()和 then()之间的文档不同jQuery 1.8? 为什么然后()返回新延迟对象?此行为完全没有记录( docs只是说它返回Deferred,而不是它是一个新的)。我知道它有这样做的实用性(即实现所有 pipe()的功能),但哲学上为什么会这样呢?根据然后()(附加处理程序)的描述,这是不必要的。 Why does the documentation between pipe() and then() differ as of jQuery 1.8?Why does then() return a new deferred object? This behavior is completely undocumented (the docs just say it returns a Deferred, not that it's a new one). I understand that it has utility in doing so (namely to implement all of pipe()'s features), but philosophically why is it the case? It's unnecessary given the description of then() (to attach handlers). 更新 我甚至会说然后() docs具有误导性和不准确性:I'll even go so far as to say the then() docs are misleading and inaccurate:可能它只是模糊,但它暗示它返回您调用然后()的延迟对象进行链接,实际上它返回一个全新的对象.... Maybe it's just vague, but it implies it returns the deferred object you called then() on for chaining, when in reality it returns an entirely new object.... 再次更新 似乎文档错误/过时了!所以这就解释了为什么文档没有提到它们是同一个东西。但是,我的第二个问题仍然存在。原因是然后()返回一个 new ,只是为了它和 pipe()可以相同吗?Seems the documentation is simply wrong/out of date! So that answers why the documentation makes no mention of them being the same thing. However, my second question still stands. Is the reason then() returns a new deferred simply so that it and pipe() can be made equivalent?推荐答案 jQuery 1.8的文档更新尚未在线。The documentation update for jQuery 1.8 is not online yet.根据最近的博客文章: 更新:是, then()返回一个新的 Deferred ,因为它相当于 pipe()现在。我非常有信心文档更新很快会澄清这一点。Update: Yes, then() returns a new Deferred because it is equivalent to pipe() now. I'm pretty confident the documentation update will clarify this soon. 完整性的进一步更新:最近更新了文档,现在说 pipe():Further update for completeness: The documentation was recently updated and now says for pipe():对于 then(): [ ...] 从jQuery 1.8开始, deferred.then()方法返回一个新的promise,它可以通过函数过滤延迟的状态和值,替换现在已弃用的 deferred.pipe()方法。 doneFilter 和 failFilter 函数过滤原始延期的已解决/已拒绝状态和值。 progressFilter 函数过滤对原始延迟的通知或 notifyWith 方法。这些过滤函数可以返回一个新值,将传递给promise的 .done()或 .fail()回调,或者他们可以返回另一个可观察对象(Deferred,Promise等),将其解析/拒绝状态和值传递给promise的回调。如果使用的过滤函数是 null ,或未指定,则将使用与原始文件相同的值解析或拒绝承诺。As of jQuery 1.8, the deferred.then() method returns a new promise that can filter the status and values of a deferred through a function, replacing the now-deprecated deferred.pipe() method. The doneFilter and failFilter functions filter the original deferred's resolved / rejected status and values. The progressFilter function filters any calls to the original deferred's notify or notifyWith methods. These filter functions can return a new value to be passed along to the promise's .done() or .fail() callbacks, or they can return another observable object (Deferred, Promise, etc) which will pass its resolved / rejected status and values to the promise's callbacks. If the filter function used is null, or not specified, the promise will be resolved or rejected with the same values as the original. 这篇关于在jQuery 1.8中使用pipe()和then()文档与现实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 16:50