0

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.

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • 2
    Possible duplicate of [Angular $http service- success(), error(), then() methods](http://stackoverflow.com/questions/27287731/angular-http-service-success-error-then-methods) – Italo Ayres May 23 '16 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 May 23 '16 at 13:35

6 Answers6

4

You should be using then as the success and error have been deprecated.

https://docs.angularjs.org/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.

Ant Kennedy
  • 1,230
  • 7
  • 13
  • can you please add couple of lines about what is Promise – Kgn-web May 23 '16 at 13:35
  • Have a look at the es6 documentation - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise. They are one method for getting out of Callback Hell, allowing you to return a promise and essentially chaining the `.then(fn).then(fn).catch(errFn)` – Ant Kennedy May 23 '16 at 13:37
  • 1
    Don't quote me on this but my guess would be that `.success()` is faster than a promise as you are simply calling a callback function. Whereas when creating a promise a new Promise instance is created and then the function called. But in reality you are not likely to notice the difference. – Ant Kennedy May 23 '16 at 13:53
2

.then( ) call returns a promise while .success( ) is more traditional way of registering callbacks and it doesn't return a promise.

Moreover .then() is commonly 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.

Denny John
  • 464
  • 7
  • 20
  • is there anything such, .success() is faster compared to .then() OR nothing such? – Kgn-web May 23 '16 at 13:47
  • 1
    No,nothing like that.Moreover I have seen many of the tutorials suggesting us to use .then() instead of .success().Please read this document http://www.syntaxsuccess.com/viewarticle/angular-promises%E2%80%93then-vs-success – Denny John May 23 '16 at 13:50
  • jQuery's `ajax()` method returns their own object, `jqXHR`, and each call to methods like success,then,always will continue returning the `jqXHR` object meaning you can continue chaining like `success().then()` – Patrick Evans May 23 '16 at 13:50
  • As for Angular's HttpPromise `success()` does return a promise, the original promise that was created, `.then` though returns a new promise – Patrick Evans May 23 '16 at 14:00
1

I would recommend 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.

mdickin
  • 2,365
  • 21
  • 27
1

This is a great article which helps you to understand Promise

http://andyshora.com/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.

Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29
0

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.com/someAPI').then(function (results) {
    // On successful promise
    doSomethingHere();
}, function (error) {
    // On failed promise
    handleError();
});
csukcc
  • 752
  • 7
  • 8
0

.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 completed 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.