角度更好.success()或.then()

时间:2022-12-16 12:48:33

I am making an ajax call to server to fetch some data.

我正在对服务器进行ajax调用以获取一些数据。

$.ajax(
{
url: "myserver",
method: "GET",
}.success(function(data)
{ }
.error(function(e)
{ }
)

I have been reading about .then().

我一直在阅读.then()。

Is there any performance benefit of using .then() over .success()?

在.success()上使用.then()有什么性能优势吗?

Is there any particular scenario where I should use .then() and .success()?

是否有任何特殊情况我应该使用.then()和.success()?

Plus, whoever answers,please brief me in short What is Promises.

另外,无论谁回答,请简要介绍一下什么是承诺。

5 个解决方案

#1


2  

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

您应该使用,因为已弃用成功和错误。

https://docs.angularjs.org/api/ng/service/$http

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.

$ http遗留承诺方法成功和错误已被弃用。请改用标准方法。如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。

#2


2  

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

.then()调用返回一个promise,而.success()是更传统的注册回调方式,它不返回一个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.

此外,.then()通常用于需要链接promises的地方,而.success()方法是一种简化的方便方法,当你不需要链接调用或使用promise API时。

#3


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.

我建议使用.then()和.catch()。这些方法符合CommonJS标准。当您使用其他Promise库时,他们更有可能使用这两种方法。

I would also avoid using the .then(successCallback, failureCallback) approach, as it is not standard and less obvious.

我也会避免使用.then(successCallback,failureCallback)方法,因为它不是标准的,也不那么明显。

#4


1  

This is a great article which helps you to understand Promise

这是一篇很棒的文章,可以帮助您理解Promise

http://andyshora.com/promises-angularjs-explained-as-cartoon.html

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.

2之间的主要区别在于.then()调用返回一个promise(使用从回调返回的值解析),而.success()是更传统的注册回调方式而不返回promise。

#5


0  

I personally prefer .then(), here is a very good blog on why .success() is not preferred.

我个人更喜欢.then(),这里有一个非常好的博客,为什么.success()不是首选。

For an API call I would do something like this:

对于API调用,我会做这样的事情:

$http.get('www.domain.com/someAPI').then(function (results) {
    // On successful promise
    doSomethingHere();
}, function (error) {
    // On failed promise
    handleError();
});

#1


2  

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

您应该使用,因为已弃用成功和错误。

https://docs.angularjs.org/api/ng/service/$http

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.

$ http遗留承诺方法成功和错误已被弃用。请改用标准方法。如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。

#2


2  

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

.then()调用返回一个promise,而.success()是更传统的注册回调方式,它不返回一个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.

此外,.then()通常用于需要链接promises的地方,而.success()方法是一种简化的方便方法,当你不需要链接调用或使用promise API时。

#3


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.

我建议使用.then()和.catch()。这些方法符合CommonJS标准。当您使用其他Promise库时,他们更有可能使用这两种方法。

I would also avoid using the .then(successCallback, failureCallback) approach, as it is not standard and less obvious.

我也会避免使用.then(successCallback,failureCallback)方法,因为它不是标准的,也不那么明显。

#4


1  

This is a great article which helps you to understand Promise

这是一篇很棒的文章,可以帮助您理解Promise

http://andyshora.com/promises-angularjs-explained-as-cartoon.html

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.

2之间的主要区别在于.then()调用返回一个promise(使用从回调返回的值解析),而.success()是更传统的注册回调方式而不返回promise。

#5


0  

I personally prefer .then(), here is a very good blog on why .success() is not preferred.

我个人更喜欢.then(),这里有一个非常好的博客,为什么.success()不是首选。

For an API call I would do something like this:

对于API调用,我会做这样的事情:

$http.get('www.domain.com/someAPI').then(function (results) {
    // On successful promise
    doSomethingHere();
}, function (error) {
    // On failed promise
    handleError();
});