Promise和AJAX有什么区别? [英] What's the difference between Promise and AJAX?

查看:1310
本文介绍了Promise和AJAX有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

promise和AJAX调用都是异步操作.两者都可以进行GET/POST请求. << 这是错误的陈述

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

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

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

另外,还有一件事:

最近,我遇到了一个承诺,其中包含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.

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

此处使用jQuery.而且AJAX调用具有Promise行为和属性.我没有早点得到,但是这是我的想法: 我们可以在承诺"中做点事情.然后使用AJAX调用,并在done函数中传递已解决的Promise逻辑.具体来说,在此示例中没有任何内容.

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.

现在我看到我已经混淆了两者.它们几乎是2种不同的东西.仅仅因为它们是异步的,并不意味着它们是可互换的.

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:

承诺反模式

推荐答案

您对Promise和Ajax调用感到困惑.它们有点像苹果和小刀.您可以用刀切一个苹果,而刀是可以应用到苹果上的工具,但是两者是完全不同的.

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调用是一种特定的异步操作,可以与传统的回调接口一起使用,也可以包装在Promise接口中.

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.

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

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

Ajax调用是一种特定类型的异步操作.您可以使用 XMLHttpRequest接口通过传统的回调进行Ajax调用,或者您可以使用带有 fetch()界面.

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.为什么 将异步操作放到异步操作中?就像放 面包三明治中的面包.

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.

您没有显示您正在谈论的特定代码,但是有时您想启动异步操作1,然后在完成异步操作后,想要他们启动异步操作2(通常使用第一个操作的结果)一).在这种情况下,通常将一个嵌套在另一个内部.

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.

您的代码示例在这里:

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

被认为是一个承诺反模式.这里没有理由创建新的诺言,因为$.getJSON()已经返回了您可以返回的诺言.您可以改为:

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:

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

或者,如果您要将非标准的jQuery Promise"转换"为标准Promise,则可以执行以下操作:

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

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

这篇关于Promise和AJAX有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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