node.js - 在 Promise 中使用 async-await
问题描述
.then(async (rows) => {
//some code
response = await sendEmail(email);
}
你好,如果我们引用另一个接口发送电子邮件,是否可以让 promises 中的 then 方法异步?
解决方案
虽然这可行,但 IMOasync/await
与承诺链接混合使用是一种不好的风格。为什么不只是
fooPromise()
.then(rows => {
...
return sendEmail(email);
})
.then(response => {
...
})
或者
async function foo() {
const rows = await fooPromise();
...
const response = await sendEmail(email);
...
}
即,选择一种您更喜欢的方式并坚持下去。