同步等待 异步等待_异步/等待入门

同步等待 异步等待

by Jamund Ferguson

通过贾蒙德·弗格森(Jamund Ferguson)

异步/等待入门 (Getting Started with Async/Await)

Inspired by the Zeit team’s post on the subject, my team at PayPal recently migrated our main server-side codebase to use Async/Await. I’m excited to share with you some of things we learned along the way.

Zeit团队关于该主题的帖子的启发,我在PayPal的团队最近将我们的主要服务器端代码库迁移为使用Async / Await。 我很高兴与您分享我们在此过程中学到的一些知识。

Let’s start with some terminology:

让我们从一些术语开始:

  • Async function

    异步功能

  • Await keyword

    等待关键字

People usually say async/await which is lovely and nice really, but you should know that they aren’t the same thing. There are async functions and there is the await keyword. They are certainly tied together in some ways, but async functions in particular can be used without await. How is that?

人们通常会说async / await确实很不错,但是您应该知道它们不是同一回事。 有异步功能 还有await关键字 。 它们肯定以某种方式捆绑在一起,但是特别是可以使用异步功能而无需等待。 那个怎么样?

异步函数返回一个承诺 (Async Functions Return a Promise)

When you create a function with the async keyword that function will always return a Promise. When you return inside your async function it wraps your value in a Promise.

使用async关键字创建函数时,该函数将始终返回Promise。 当您在异步函数内部返回时,它将您的值包装在Promise中。

Even if your code throws inside an async function it won’t automatically bubble up, instead it will return a rejected promise

即使您的代码抛出异步函数,它也不会自动冒泡,而是会返回被拒绝的承诺

异步功能是唯一可以使用Await的地方 (Async Functions are the only Place where you can use Await)

In addition to converting your returns into a Promise, an async function is also special in that it’s the only place where you can use the await keyword.*

除了将退货转换为Promise之外,异步功能也很特别,因为它是唯一可以使用await关键字的地方。*

What is the await keyword? Await lets you pause the execution of an async function until it receives the results of a promise. This lets you write async code that reads in the order that it’s executed.

什么是await关键字? 通过Await,您可以暂停执行异步功能,直到接收到Promise的结果为止。 这使您可以编写异步代码,并按执行顺序进行读取。

Await allows you to write asynchronous code with no callbacks at all. This makes your code much more readable. And await works with any promise, not just promises created by async functions.

Await允许您编写完全没有回调的异步代码。 这使您的代码更具可读性。 并且await与任何promise一起使用,而不仅仅是异步函数创建的promise。

异步函数中的错误处理 (Error Handling in Async Functions)

Because an async function is a Promise, when you throw inside of an async function it’s swallowed up and returned as a rejected Promise.

因为异步函数是Promise ,所以当您异步函数放入内部时,它会被吞没并作为拒绝的Promise返回。

If you are using await to call the Promise you can wrap it in try/catch or you’ll need to add a catch handler to the returned Promise.

如果您正在使用await来调用Promise ,则可以将其包装在try / catch中,或者需要将catch处理程序添加到返回的Promise中。

Promise errors usually bubble up to their parent, so you usually only need that try/catch on your top-level Promise.

Promise错误通常会冒充其父级,因此您通常只需要在顶级Promise上进行try / catch

全部放在一起 (Putting it all Together)

Taking advantage of the error handling properties of promises and the concise syntax of async functions can yield some powerfully simple results.

利用promise的错误处理属性和异步函数的简洁语法可以产生一些功能强大的简单结果。

In this simplified example you can see how one might take advantage of the inherent error handling capabilities async functions to simplify error handling in an Express app

在这个简化的示例中,您可以看到人们如何利用固有的错误处理功能异步功能来简化Express应用程序中的错误处理

On my team at PayPal we usually handle errors with next(err). However with async/await we can simply throw errors anywhere in the code and the router will forward them to the next function provided by Express. This is a huge simplification.

在我在PayPal的团队中,我们通常会使用next(err)处理错误 但是,使用async / await,我们可以在代码中的任何地方简单地抛出错误,路由器会将它们转发到Express提供的下一个功能 这是一个巨大的简化。

Moving from callbacks to promises and async/await has condensed error handling in our app and will improve comprehension for our more complicated code paths. It took me a couple of hours to migrate most of our routes from plain callbacks to this new approach. Really the only thing you need to get started is a solid knowledge of Promises and an understanding of how to setup babel.

从回调转移到Promise和异步/等待已使我们的应用程序中的错误处理更加紧凑,并将提高对更复杂的代码路径的理解。 我花了几个小时才能将我们的大多数路由从简单的回调迁移到这种新方法。 确实,您唯一需要入门的是对Promises扎实知识以及对如何设置babel的理解。

I eagerly await hearing your experiences with these newfangled functions and believe they are going to be one of my favorite tools in the JavaScript toolbox moving forward.

我热切地等待着您听到这些新奇功能的经验,并相信它们将成为我将来JavaScript工具箱中我最喜欢的工具之一。

* Top-level await is not allowed in the current spec (Though there has been some discussion about possibly allowing this in the future).

*当前规范中不允许顶层等待( 尽管已经有关于将来可能允许这样做的讨论 )。

翻译自: https://www.freecodecamp.org/news/getting-started-with-async-await-b66385983875/

同步等待 异步等待

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值