What is the difference between .resolve() and .promise()?

混江龙づ霸主 提交于 2019-12-03 05:36:02

问题


I'm digging into jQuery's deferred features. I've tried several examples on the internet. I understood its concept, but I don't understand the difference between resolve() and promise().

What are differences between resolve() and promise()?


回答1:


Both resolve() and promise() are methods on the jQuery Deferred object.

First a snippet from the jQuery documentation about Deferred:

One model for understanding Deferred is to think of it as a chain-aware function wrapper. The deferred.then(), deferred.done(), and deferred.fail() methods specify the functions to be called and the deferred.resolve(args) or deferred.reject(args) methods “call” the functions with the arguments you supply.

With that in mind, promise() returns an object that is very similar to the Deferred object except that it only has then(), done(), and fail() methods and does not have resolve() or reject().

From the blog post m-sharp referred to regarding promise():

This is useful when you want to give to the calling API something to subscribe to, but not the ability to resolve or reject the deferred.



来源:https://stackoverflow.com/questions/7641964/what-is-the-difference-between-resolve-and-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!