簡體   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