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

2020-06-17 07:11:52易采站长站整理

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


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 的这篇文章。