Promise和AJAX之间有什么区别?

时间:2021-09-24 15:50:35

Both promises and AJAX calls are asynchronous operations. A GET/POST request could be made with both. << Edit: that's a WRONG statement

promise和AJAX调用都是异步操作。两者都可以请求GET/POST。<< Edit:这是错误的语句

So what's the difference between them? And when would be best to use one instead of the other?

它们之间有什么区别呢?什么时候最好使用一个而不是另一个?

Also, one more thing:

另外,还有一件事:

Recently I encountered a promise which had an AJAX in its body. Why put an async operation inside an async operation? That's like putting a bread loaf in a bread sandwich.

最近,我遇到了一个在其主体中包含AJAX的承诺。为什么要将异步操作放在异步操作中?这就像在面包三明治里放一块面包。

function threadsGet() {
return new Promise((resolve, reject) => {
  $.getJSON('api/threads')
    .done(resolve)
    .fail(reject);
    })
}

jQuery is used here. And the AJAX call has Promise behavior and properties. I didn't get that earlier but here are my thoughts: We can do something in the Promise. Then use the AJAX call and in the done function pass the resolved Promise logic. Specifically in this example there is none.

这里使用jQuery。而AJAX调用具有承诺行为和属性。我之前没说过,但我的想法是:我们可以在承诺中有所作为。然后使用AJAX调用,在done函数中传递已解析的承诺逻辑。特别是在这个例子中没有。

Now I see that I had confused both. They're pretty much 2 different things. Just because they're asynchronous, doesn't mean they're interchangeable.

现在我发现我把两者都搞混了。它们是两种不同的东西。仅仅因为它们是异步的,并不意味着它们是可互换的。

==============

= = = = = = = = = = = = = =

EDIT 2: Just some materials I found useful:

编辑2:我发现一些有用的材料:

Promise Anti-Patterns

承诺的反模式

1 个解决方案

#1


29  

You are confused about promises and Ajax calls. They are kind of like apples and knives. You can cut an apple with knife and the knife is a tool that can be applied to an apple, but the two are very different things.

您对许诺和Ajax调用感到困惑。它们有点像苹果和刀子。你可以用刀切苹果,刀是一种可以应用到苹果上的工具,但两者是截然不同的东西。

Promises are a tool for managing asynchronous operations. They keep track of when asynchronous operations complete and what their results are and let you coordinate that completion and those results (including error conditions) with other code or other asynchronous operations. They aren't actually asynchronous operations in themselves. An Ajax call is a specific asynchronous operation that can be used with with a traditional callback interface or wrapped in a promise interface.

承诺是管理异步操作的工具。它们跟踪异步操作何时完成以及它们的结果是什么,并允许您将完成和那些结果(包括错误条件)与其他代码或其他异步操作协调起来。它们本身并不是异步操作。Ajax调用是一种特定的异步操作,它可以与传统的回调接口一起使用,或者封装在一个承诺接口中。

So what's the difference between them? And when would be best to use one instead of the other?

它们之间有什么区别呢?什么时候最好使用一个而不是另一个?

An Ajax call is a specific type of asynchronous operation. You can make an Ajax call either with a traditional callback using the XMLHttpRequest interface or you can make an Ajax call (in modern browsers), using a promise with the fetch() interface.

Ajax调用是一种特定类型的异步操作。可以使用XMLHttpRequest接口使用传统回调进行Ajax调用,也可以使用fetch()接口的承诺进行Ajax调用(在现代浏览器中)。

Recently I encountered a promise which had an AJAX in its body. Why put an async operation inside an async operation? That's like putting a bread loaf in a bread sandwich.

最近,我遇到了一个在其主体中包含AJAX的承诺。为什么要将异步操作放在异步操作中?这就像在面包三明治里放一块面包。

You didn't show the specific code you were talking about, but sometimes you want to start async operation 1 and then when that async operation is done, you want to them start async operation 2 (often using the results of the first one). In that case, you will typically nest one inside the other.

您没有显示您所讨论的特定代码,但有时您想启动异步操作1,然后在完成异步操作之后,您希望它们启动异步操作2(通常使用第一个操作的结果)。在这种情况下,您通常将一个嵌在另一个中。


Your code example here:

代码示例:

function threadsGet() {
    return new Promise((resolve, reject) => {
      $.getJSON('api/threads')
        .done(resolve)
        .fail(reject);
      })
}

is considered a promise anti-pattern. There's no reason to create a new promise here because $.getJSON() already returns a promise which you can return. You can just do this instead:

被认为是反模式的承诺。这里没有理由在这里创建一个新的承诺,因为$. getjson()已经返回了一个可以返回的承诺。你可以这样做:

function threadsGet() {
    return $.getJSON('api/threads');
}

Or, if you want to "cast" the somewhat non-standard jQuery promise to a standard promise, you can do this:

或者,如果你想要“cast”这个有点非标准的jQuery承诺到一个标准的承诺,你可以这样做:

function threadsGet() {
    return Promise.resolve($.getJSON('api/threads'));
}

#1


29  

You are confused about promises and Ajax calls. They are kind of like apples and knives. You can cut an apple with knife and the knife is a tool that can be applied to an apple, but the two are very different things.

您对许诺和Ajax调用感到困惑。它们有点像苹果和刀子。你可以用刀切苹果,刀是一种可以应用到苹果上的工具,但两者是截然不同的东西。

Promises are a tool for managing asynchronous operations. They keep track of when asynchronous operations complete and what their results are and let you coordinate that completion and those results (including error conditions) with other code or other asynchronous operations. They aren't actually asynchronous operations in themselves. An Ajax call is a specific asynchronous operation that can be used with with a traditional callback interface or wrapped in a promise interface.

承诺是管理异步操作的工具。它们跟踪异步操作何时完成以及它们的结果是什么,并允许您将完成和那些结果(包括错误条件)与其他代码或其他异步操作协调起来。它们本身并不是异步操作。Ajax调用是一种特定的异步操作,它可以与传统的回调接口一起使用,或者封装在一个承诺接口中。

So what's the difference between them? And when would be best to use one instead of the other?

它们之间有什么区别呢?什么时候最好使用一个而不是另一个?

An Ajax call is a specific type of asynchronous operation. You can make an Ajax call either with a traditional callback using the XMLHttpRequest interface or you can make an Ajax call (in modern browsers), using a promise with the fetch() interface.

Ajax调用是一种特定类型的异步操作。可以使用XMLHttpRequest接口使用传统回调进行Ajax调用,也可以使用fetch()接口的承诺进行Ajax调用(在现代浏览器中)。

Recently I encountered a promise which had an AJAX in its body. Why put an async operation inside an async operation? That's like putting a bread loaf in a bread sandwich.

最近,我遇到了一个在其主体中包含AJAX的承诺。为什么要将异步操作放在异步操作中?这就像在面包三明治里放一块面包。

You didn't show the specific code you were talking about, but sometimes you want to start async operation 1 and then when that async operation is done, you want to them start async operation 2 (often using the results of the first one). In that case, you will typically nest one inside the other.

您没有显示您所讨论的特定代码,但有时您想启动异步操作1,然后在完成异步操作之后,您希望它们启动异步操作2(通常使用第一个操作的结果)。在这种情况下,您通常将一个嵌在另一个中。


Your code example here:

代码示例:

function threadsGet() {
    return new Promise((resolve, reject) => {
      $.getJSON('api/threads')
        .done(resolve)
        .fail(reject);
      })
}

is considered a promise anti-pattern. There's no reason to create a new promise here because $.getJSON() already returns a promise which you can return. You can just do this instead:

被认为是反模式的承诺。这里没有理由在这里创建一个新的承诺,因为$. getjson()已经返回了一个可以返回的承诺。你可以这样做:

function threadsGet() {
    return $.getJSON('api/threads');
}

Or, if you want to "cast" the somewhat non-standard jQuery promise to a standard promise, you can do this:

或者,如果你想要“cast”这个有点非标准的jQuery承诺到一个标准的承诺,你可以这样做:

function threadsGet() {
    return Promise.resolve($.getJSON('api/threads'));
}