Node.js中async的用法是什么

 2618

在nodejs中,async关键字可以用于定义一个函数,当async函数被调用时,会返回一个Promise,当async函数返回一个值时,这个Promise就会被实现,当函数抛出一个错误时,Promise就会被拒绝。


Node.js中async的用法是什么


nodejs中async的用法是什么

1、什么是 async 函数

利用 async 函数,你可以把基于 Promise 的异步代码写得就像同步代码一样。一旦你使用 async 关键字来定义了一个函数,那你就可以在这个函数内使用 await 关键字。当一个 async 函数被调用时,它会返回一个 Promise。当这个 async 函数返回一个值时,那个 Promise 就会被实现;而如果函数中抛出一个错误,那么 Promise 就会被拒绝。

await 关键字可以被用来等待一个 Promise 被解决并返回其实现的值。如果传给 await 的值不是一个 Promise,那它会把这个值转化为一个已解决的 Promise。

  1. const rp = require('request-promise')
  2. async function main () {
  3.     const result = await rp('https://google.com')
  4.     const twenty = await 20
  5.     // 睡个1秒钟
  6.     await new Promise (resolve => {
  7.         setTimeout(resolve, 1000)
  8.     })
  9.     return result
  10. }
  11. main()
  12. .then(console.log)
  13. .catch(console.error)


2、向 async 函数迁移

如果你的 Node.js 应用已经在使用Promise,那你只需要把原先的链式调用改写为对你的这些 Promise 进行 await。

如果你的应用还在使用回调函数,那你应该以渐进的方式转向使用 async 函数。你可以在开发一些新功能的时候使用这项新技术。当你必须调用一些旧有的代码时,你可以简单地把它们包裹成为 Promise 再用新的方式调用。

要做到这一点,你可以使用内建的 util.promisify方法:

  1. const util = require('util')
  2. const {readFile} = require('fs')
  3. const readFileAsync = util.promisify(readFile)
  4. async function main () {
  5.     const result = await readFileAsync('.gitignore')
  6.     return result
  7. }
  8. main()
  9. .then(console.log)
  10. .catch(console.error)


3、Async 函数的最佳实践

3.1 在 express 中使用 async 函数

express 本来就支持 Promise,所以在 express 中使用 async 函数是比较简单的:

  1. const express = require('express')
  2. const app = express()
  3. app.get('/', async (request, response) => {
  4.  // 在这里等待 Promise
  5.  // 如果你只是在等待一个单独的 Promise,你其实可以直接将将它作为返回值返回,不需要使用 await 去等待。
  6.  const result = await getContent()
  7.  response.send(result)
  8. })
  9. app.listen(process.env.PORT)

但正如 Keith Smith 所指出的,上面这个例子有一个严重的问题——如果 Promise 最终被拒绝,由于这里没有进行错误处理,那这个 express 路由处理器就会被挂起。

为了修正这个问题,你应该把你的异步处理器包裹在一个对错误进行处理的函数中:

  1. const awaitHandlerFactory = (middleware) => {
  2.  return async (req, res, next) => {
  3.   try {
  4.    await middleware(req, res, next)
  5.   } catch (err) {
  6.    next(err)
  7.   }
  8.  }
  9. }
  10. // 然后这样使用:
  11. app.get('/', awaitHandlerFactory(async (request, response) => {
  12.  const result = await getContent()
  13.  response.send(result)
  14. }))


3.2 并行执行

比如说你正在编写这样一个程序,一个操作需要两个输入,其中一个来自于数据库,另一个则来自于一个外部服务:

  1. async function main () {
  2.  const user = await Users.fetch(userId)
  3.  const product = await Products.fetch(productId)
  4.  await makePurchase(user, product)
  5. }

在这个例子中,会发生什么呢?

你的代码会首先去获取 user,

然后获取 product,

最后再进行支付。

如你所见,由于前两步之间并没有相互依赖关系,其实你完全可以将它们并行执行。这里,你应该使用 Promise.all 方法:

  1. async function main () {
  2.  const [user, product] = await Promise.all([
  3.   Users.fetch(userId),
  4.   Products.fetch(productId)
  5.  ])
  6.  await makePurchase(user, product)
  7. }

而有时候,你只需要其中最快被解决的 Promise 的返回值——这时,你可以使用 Promise.race 方法。


3.3 错误处理

考虑下面这个例子:

  1. async function main () {
  2.  await new Promise((resolve, reject) => {
  3.   reject(new Error('error'))
  4.  })
  5. }
  6. main()
  7.  .then(console.log)

当执行这段代码的时候,你会看到类似这样的信息:

(node:69738) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: error

(node:69738) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

在较新的 Node.js 版本中,如果 Promise 被拒绝且未得到处理,整个 Node.js 进程就会被中断。因此必要的时候你应该使用 try-catch:

  1. const util = require('util')
  2. async function main () {
  3.  try {
  4.   await new Promise((resolve, reject) => {
  5.    reject(new Error('


TAG标签:
本文网址:https://www.zztuku.com/detail-10708.html
站长图库 - Node.js中async的用法是什么
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐