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
Add a ment  | 

6 Answers 6

Reset to default 4

You 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