javascript - express router 不能等待一个 promise 来解析吗?

标签 javascript node.js express async-await

当我使用 express router 中的以下代码时

var anotherasyncfunction = async() => {
    var value = await settings.get("config"); // this is another async from a module.
    console.log(value);
}
app.get("/api", async(req, res) => {
    await anotherasyncfunction(); // This is another async function
    //more code
});      

我收到以下错误。

错误:发送后无法设置 header 。

当我删除行时

await anotherasyncfunction(); 

没有错误。

如何解决?我不能等待在 Express 路由器中使用 await 来解决的 promise 吗?

最佳答案

您可能对 express-promise-router 感兴趣,它封装了 express 路由器并允许您使用 async/await。

https://www.npmjs.com/package/express-promise-router

关于javascript - express router 不能等待一个 promise 来解析吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52324273/

相关文章:

javascript - 如果 float DIV 要下拉一行,则将其拆分为 2

javascript - Node.js 在这里适用吗?

r - 在 AWS 上使用 Rserve 在 Node.js 和 R 之间进行跨平台通信时出错。错误:connect ETIMEDOUT

node.js - Mongoose save() 实际上并没有保存

javascript - 为什么从我的 jQuery.getJSON() 方法调用失败方法?

javascript - 无法在 apache 上加载 javascript 文件

javascript - Kendo UI - 如何引用控件?

javascript - 关于 Ember 中的自定义构建

node.js - Express/MongoDB - 数据库操作期间的意外行为

javascript - Express:获取完整路线参数