JavaScript promise和async await有什么区别? [英] What is the difference between JavaScript promises and async await?

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

问题描述

我一直在使用 ECMAScript  6 和ECMAScript  7功能(感谢Babel)在我的应用程序中-移动和网络.

I have been using ECMAScript 6 and ECMAScript 7 features already (thanks to Babel) in my applications - both mobile and web.

显然,第一步是达到ECMAScript 6级.我学习了许多异步模式,promise(确实很有希望),生成器(不确定为什么使用*符号)等. 其中,诺言非常适合我的目的.而且我已经在我的应用程序中使用它们很多次了.

The first step obviously was to ECMAScript 6 levels. I learnt many async patterns, the promises (which are really promising), generators (not sure why the * symbol), etc. Out of these, promises suited my purpose pretty well. And I have been using them in my applications quite a lot.

这是我如何实现基本诺言的示例/伪代码-

Here is an example/pseudocode of how I have implemented a basic promise-

var myPromise = new Promise(
    function (resolve,reject) {
      var x = MyDataStore(myObj);
      resolve(x);
    });

myPromise.then(
  function (x) {
    init(x);
});

随着时间的流逝,我遇到了ECMAScript 7个功能,其中之一是ASYNCAWAIT关键字/功能.这些结合在一起创造了很大的奇迹.我已经开始用async & await代替我的一些承诺.它们似乎为编程风格增添了巨大价值.

As time passed, I came across ECMAScript 7 features, and one of them being ASYNC and AWAIT keywords/functions. These in conjunction do great wonders. I have started to replace some of my promises with async & await. They seem to add great value to programming style.

同样,这是我的异步,等待功能的伪代码-

Again, here is a pseudocode of how my async, await function looks like-

async function myAsyncFunction (myObj) {
    var x = new MyDataStore(myObj);
    return await x.init();
}
var returnVal = await myAsyncFunction(obj);

撇开语法错误(如果有的话),他们俩做的事情完全一样,这就是我的感受.我几乎可以用异步来代替我的大部分诺言.

Keeping the syntax errors (if any) aside, both of them do the exact same thing is what I feel. I have almost been able to replace most of my promises with async,awaits.

为什么诺言做类似的工作需要异步等待?

Why is async,await needed when promises do a similar job?

异步,等待解决更大的问题吗?还是只是用于回调地狱的另一种解决方案?

Does async,await solve a bigger problem? Or was it just a different solution to callback hell?

正如我之前所说,我可以使用promise和async,await来解决相同的问题.异步等待解决了什么具体问题?

As I said earlier, I am able to use promises and async,await to solve the same problem. Is there anything specific that async await solved?

其他说明:

我一直在我的React项目和Node.js模块中广泛使用async,awaits和promises. React特别是早起的鸟儿,并采用了许多ECMAScript  6和ECMAScript  7功能.

I have been using async,awaits and promises in my React projects and Node.js modules extensively. React especially have been an early bird and adopted a lot of ECMAScript 6 and ECMAScript 7 features.

推荐答案

为什么Promises做类似的工作需要异步,等待?异步等待解决更大的问题吗?

Why is async,await needed when Promises does similar job? Does async,await solve a bigger problem?

async/await只是给您一种异步代码的同步感觉.这是一种非常优雅的语法糖.

async/await simply gives you a synchronous feel to asynchronous code. It's a very elegant form of syntactical sugar.

对于简单的查询和数据操作,Promises可能很简单,但是如果您遇到复杂的数据操作且不涉及任何内容的场景,那么只要代码看起来为尽管它是同步的(换句话说,语法本身就是async/await可以解决的偶然复杂性"形式).

For simple queries and data manipulation, Promises can be simple, but if you run into scenarios where there's complex data manipulation and whatnot involved, it's easier to understand what's going on if the code simply looks as though it's synchronous (to put it another way, syntax in and of itself is a form of "incidental complexity" that async/await can get around).

如果您有兴趣知道,可以使用 co 之类的库(以及其他生成器)的感觉.已经开发出了类似的方法来解决async/await最终(本机)解决的问题.

If you're interested to know, you can use a library like co (alongside generators) to give the same sort of feel. Things like this have been developed to solve the problem that async/await ultimately solves (natively).

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

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