Promise 和 AJAX 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39751395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:31:20  来源:igfitidea点击:

What's the difference between Promise and AJAX?

ajaxasynchronouspromise

提问by Bruno

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

承诺和 AJAX 调用都是异步操作。可以同时使用 GET/POST 请求。<<编辑:这是一个错误的陈述

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 的 promise。为什么将异步操作放在异步操作中?这就像在面包三明治里放一条面包。

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 donefunction pass the resolved Promise logic. Specifically in this example there is none.

这里使用了 jQuery。并且 AJAX 调用具有 Promise 行为和属性。我之前没有明白,但这是我的想法:我们可以在 Promise 中做一些事情。然后使用 AJAX 调用并在done函数中传递已解决的 Promise 逻辑。特别是在这个例子中没有。

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

Promise 反模式

回答by jfriend00

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.

您对 promise 和 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.

Promise 是一种管理异步操作的工具。它们会跟踪异步操作何时完成以及它们的结果是什么,并让您协调该完成和这些结果(包括错误条件)与其他代码或其他异步操作。它们本身实际上并不是异步操作。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 XMLHttpRequestinterfaceor you can make an Ajax call (in modern browsers), using a promise with the fetch()interface.

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

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 的 promise。为什么将异步操作放在异步操作中?这就像在面包三明治里放一条面包。

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:

或者,如果您想将有点非标准的 jQuery 承诺“转换”为标准承诺,您可以这样做:

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