admin管理员组文章数量:1401134
I am making an ajax call to server to fetch some data.
$.ajax(
{
url: "myserver",
method: "GET",
}.success(function(data)
{ }
.error(function(e)
{ }
)
I have been reading about .then().
Is there any performance benefit of using .then() over .success()?
Is there any particular scenario where I should use .then() and .success()?
Plus, whoever answers,please brief me in short What is Promises.
I am making an ajax call to server to fetch some data.
$.ajax(
{
url: "myserver",
method: "GET",
}.success(function(data)
{ }
.error(function(e)
{ }
)
I have been reading about .then().
Is there any performance benefit of using .then() over .success()?
Is there any particular scenario where I should use .then() and .success()?
Plus, whoever answers,please brief me in short What is Promises.
Share Improve this question asked May 23, 2016 at 13:29 Kgn-webKgn-web 7,58531 gold badges105 silver badges176 bronze badges 2- 2 Possible duplicate of Angular $http service- success(), error(), then() methods – Italo Ayres Commented May 23, 2016 at 13:34
-
1
@ItaloAyres He's actually asking what the difference is between
$.ajax
and promises. There's more to this question. – Reinstate Monica Cellio Commented May 23, 2016 at 13:35
6 Answers
Reset to default 4You should be using then
as the success
and error
have been deprecated.
https://docs.angularjs/api/ng/service/$http
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
.then( ) call returns a promise while .success( ) is more traditional way of registering callbacks and it doesn't return a promise.
Moreover .then() is monly used where you need to chain promises whereas .success() method is a streamlined, convenience method when you don't need to chain call nor work with the promise API.
I would remend using .then()
and .catch()
. Those methods are in line with the CommonJS standard. As you use other Promise libraries, it's more likely that they'll use those two methods.
I would also avoid using the .then(successCallback, failureCallback)
approach, as it is not standard and less obvious.
This is a great article which helps you to understand Promise
http://andyshora./promises-angularjs-explained-as-cartoon.html
and
The major difference between the 2 is that .then()
call returns a promise (resolved with a value returned from a callback) while .success()
is more traditional way of registering callbacks and doesn't return a promise.
I personally prefer .then()
, here is a very good blog on why .success()
is not preferred.
For an API call I would do something like this:
$http.get('www.domain./someAPI').then(function (results) {
// On successful promise
doSomethingHere();
}, function (error) {
// On failed promise
handleError();
});
.Success and .then both work same but there are lot of difference between there response unit, In angular and ajax success work same after evening thing goes pleted and you might need to fetch response data then User .success,
In .then it happens before .Success and After .Success. so if You have Loading bar in your html code use before success .then for loading... and after success use .then for hiding it.
本文标签: javascriptWhich is better success() or then() in angularStack Overflow
版权声明:本文标题:javascript - Which is better .success() or .then() in angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744294223a2599263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论