繁体   English   中英

快递路由器不能等待承诺解决吗?

[英]Can't express router wait for a promise to resolve?

当我使用快速路由器中的以下代码时

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
});      

我收到以下错误。

错误:发送后无法设置标头。

当我删除该行

await anotherasyncfunction(); 

没有错误。

如何解决? 我不能等待承诺在 express 路由器中使用 await 来解决吗?

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

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

据我了解,您想先等待“ / api”的结果,然后再运行anotherasyncfunction();。

如果我理解正确,请尝试以下代码

app.get("/api").then(async function (response) {

    console.log(response)
    await anotherasyncfunction();
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM