AJAX中的承诺与成功之间的区别 [英] Difference between Promise and Success in AJAX

查看:60
本文介绍了AJAX中的承诺与成功之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Ajax和Promise的成功回调感到困惑。

I am bit confused between success call back in Ajax and Promise.

让我试着通过一个例子来解释我的困惑。

Let me try to explain my confusion with an example.

$http.get("https://someapi.com").success(function (){})

在上面的代码中,成功的是我们的回调函数,它将在异步操作完成时执行..对吗?

In the above code, success it our callback function which is going to be executed whenever the asynchronous operation completes .. right?

现在来到Promise。

Now coming to Promise.

我们创建一个承诺来处理成功和失败(解决和拒绝)

We create a promise to handle both the Success and Failure (Resolve and Reject)

现在我的问题是,当我们在Ajax中回调供应时,为什么我们需要承诺?

Now my question, when we have call back provision in Ajax, why do we need promise?

推荐答案

使用promises的好处就是它们为异步任务提供了一个强大的通用接口。

The benefit of using promises is just that they provide a powerful, common interface for async tasks.

通用接口的主要好处(任何通用接口)是你可以编写和使用在promises上运行的更高级函数。仅举一个例子,许多promise库提供了一个函数,通常称为 all ,它接受一组promise,并返回一个封装所有promise的新promise。然后你可以编写如下内容:

The main benefit of a common interface (any common interface) is that you can write and use higher-level functions that operate on promises. As just one example, many promise libraries provide a function, often called all, that takes a set of promises, and returns a new promise that encapsulates all of them. Then you can write things like:

promise.all(promise1, promise2).then(function (value1, value2) {
    // blah blah
});

而非此金字塔代码

promise1.then(function (value1) {
    promise2.then(function (value2) {
        // blah blah
    });
});

当然,您只能使用全部如果您正在使用具有某些已知通用接口的异步任务。如果有些人有 .then 方法,有些人有 .success 方法,等等。

And of course you can only use functions like all if you're using async tasks with some known, common interface. It would be impossible if some had .then methods, some had .success methods, and so on.

承诺界面还有其他有用的属性,例如 .then promise上的方法返回一个promise - 如果你传递的回调返回一个promise, .then 方法返回一个实际等待回调返回的promise的promise。换句话说:

The promise interface has other useful properties, for instance the .then method on a promise returns a promise - and if the callback you pass it returns a promise, the .then method returns a promise that actually awaits the promise returned by the callback. In other words:

// p1 is a promise that resolves to 1, when somePromise resolves
var p1 = somePromise.then(function (value) {
    return 1;
});

// p2 is a promise that resolves to what someOtherPromise resolves to,
// not to someOtherPromise itself
var p2 = somePromise.then(function (value) {
    return someOtherPromise;
});

这使得一个接一个地进行一系列HTTP调用变得简单易行。

Which makes it nice and easy to make a series of HTTP calls, one after another.

使用promises也有一些缺点...按照惯例,它们将你的回调包装在try / catch块中,所以如果你抛出异常它会被捕获,有时候是隐形的。

There are some drawbacks to using promises too... By convention, they wrap your callback in a try/catch block, so if you throw an exception it will be caught, sometimes invisibly.

somePromise.then(function (value) {
    throw 'error';
}).catch(function (error) {
    // without this catch block, the error may be eaten invisibly
    console.error(error);
});

总而言之,promise只是一个广泛使用的界面,用于管理异步任务。如果你愿意,可以使用它,如果你不愿意,请不要使用它。

To sum up, a promise is just a widely-used interface for managing asynchronous tasks. Use it if you want, don't if you don't.

这篇关于AJAX中的承诺与成功之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆