Javascript Promises vs Async Await。区别? [英] Javascript Promises vs Async Await. Difference?

查看:136
本文介绍了Javascript Promises vs Async Await。区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中使用了ES6和ES7功能(感谢Babel) - 移动和网络。

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

显然,第一步是ES6级别。我学到了许多异步模式,承诺(真的很有前途),生成器(不知道为什么是*符号)等等,
其中,Promises非常适合我的目的。我一直在我的应用程序中使用它们。

First step obviously was to ES6 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);
});

随着时间的推移,我遇到了ES7功能,其中一个是 ASYNC AWAIT 关键字/功能。这些结合起来有很大的奇迹。我已经开始用 async&替换我的一些承诺。等待。它们似乎为编程风格增添了很多价值。

As time passed, I came across ES7 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.

同样,这里是我的async,await函数的伪代码 -

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);

保持语法错误(如果有的话),两者都做同样的事情就是我感觉。
我几乎能够用async替换我的大部分承诺。等等。

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.

我的问题是,为什么异步,等待Promise做类似的工作?
async,等待解决更大的问题吗?或者它只是回调地狱的另一种解决方案?
正如我之前所说,我能够使用Promises和Async,Await来解决同样的问题。有没有具体的Async Await解决了?

My question here is, Why is async,await needed when Promises does similar job? Does async,await solve a bigger problem? or was it just a different solution to callback hell? 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?

附加说明:
我一直在使用Async,Awaits&我的ReactJS项目中的承诺&节点模块广泛。
ReactJS尤其是早起的鸟类并采用了很多ES6& ES7功能。

Additional Notes: I have been using Async,Awaits & Promises in my ReactJS projects & Node modules extensively. ReactJS especially have been an early bird and adopted a lot of ES6 & ES7 features.

推荐答案


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

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 <​​/ code> (与发电机一起)给人以同样的感觉。这样的事情已经被开发出来解决 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 Promises vs Async Await。区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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