欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在 Node.js 中使用 async 函数的方法

程序员文章站 2022-09-01 22:49:04
借助于新版 v8 引擎,node.js 从 7.6 开始支持 async 函数特性。今年 10 月 31 日,node.js 8 也开始成为新的长期支持版本,因此你完全可以...

借助于新版 v8 引擎,node.js 从 7.6 开始支持 async 函数特性。今年 10 月 31 日,node.js 8 也开始成为新的长期支持版本,因此你完全可以放心大胆地在你的代码中使用 async 函数了。在这边文章里,我会简要地介绍一下什么是 async 函数,以及它会如何改变我们编写 node.js 应用的方式。

1 什么是 async 函数

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

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

const rp = require('request-promise')
async function main () {
 const result = await rp('https://google.com')
 const twenty = await 20
 // 睡个1秒钟
 await new promise (resolve => {
  settimeout(resolve, 1000)
 })
 return result
}
main()
 .then(console.log)
 .catch(console.error)

2 向 async 函数迁移

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

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

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

const util = require('util')
const {readfile} = require('fs')
const readfileasync = util.promisify(readfile)
async function main () {
 const result = await readfileasync('.gitignore')
 return result
}
main()
 .then(console.log)
 .catch(console.error)

3 async 函数的最佳实践

3.1 在 express 中使用 async 函数

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

const express = require('express')
const app = express()
app.get('/', async (request, response) => {
 // 在这里等待 promise
 // 如果你只是在等待一个单独的 promise,你其实可以直接将将它作为返回值返回,不需要使用 await 去等待。
 const result = await getcontent()
 response.send(result)
})
app.listen(process.env.port)

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

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

const awaithandlerfactory = (middleware) => {
 return async (req, res, next) => {
  try {
   await middleware(req, res, next)
  } catch (err) {
   next(err)
  }
 }
}
// 然后这样使用:
app.get('/', awaithandlerfactory(async (request, response) => {
 const result = await getcontent()
 response.send(result)
}))

3.2 并行执行

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

async function main () {
 const user = await users.fetch(userid)
 const product = await products.fetch(productid)
 await makepurchase(user, product)
}

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

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

然后获取 product,

最后再进行支付。

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

async function main () {
 const [user, product] = await promise.all([
  users.fetch(userid),
  products.fetch(productid)
 ])
 await makepurchase(user, product)
}

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

3.3 错误处理

考虑下面这个例子:

async function main () {
 await new promise((resolve, reject) => {
  reject(new error('error'))
 })
}
main()
 .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:

const util = require('util')
async function main () {
 try {
  await new promise((resolve, reject) => {
   reject(new error('????'))
  })
 } catch (err) {
  // 在这里处理错误
  // 根据你的需要,有时候把错误直接再抛出也是可行的
 }
}
main()
 .then(console.log)
 .catch(console.error)

可是,使用 try-catch 可能会隐藏掉一些重要的异常,比如像系统错误,你可能更想把它再抛出来。关于在什么情况下你应该将错误再次抛出,我强烈建议你去读一下 eran 的这篇文章。

3.4 更为复杂的流程控制

caolan mcmahon 的 async 是一个出现较早的用于 node.js 中异步流程控制的库。它提供了一些进行异步操作控制的帮助工具,比如:

maplimit,
filterlimit,
concatlimit,

以及 priorityqueue。

如果你不打算重新发明*,不想把同样的逻辑自己再实现一遍,并且愿意信赖这个经过实践检验的、每月下载量高达 5000 万的库,你可以结合 util.promisify 简单地重用这些函数:

const util = require('util')
const async = require('async')
const numbers = [
 1, 2, 3, 4, 5
]
maplimitasync = util.promisify(async.maplimit)
async function main () {
 return await maplimitasync(numbers, 2, (number, done) => {
  settimeout(function () {
   done(null, number * 2)
  }, 100)
 })
}
main()
 .then(console.log)
 .catch(console.error)

总结

以上所述是小编给大家介绍的在 node.js 中使用 async 函数的方法,希望对大家有所帮助