[英]Is there any advantage in nodejs calling an async function followed by an await versus a synchronous function?
[英]Nodejs any problem in calling async function without await?
我有一个端点,我想做一些用户不感兴趣的async
后处理,所以我想异步处理它们并向用户发送响应。
像这样的东西。
const createOrder = async (req,res)=>{
const order = await doCreateOrder(); //creates the order
somePostProcessing().catch(()=>{
//handle errors
});//asycn function which does some db/network operations(not interested in results)
return reply.send(order)
}
但我担心它会导致任何问题,例如 memory 泄漏(我的团队负责人这么说)等等? 在nodeJS中进行此类后期处理的最佳方式或其他选择是什么?
忽略 promise 的结果不是问题。 您是否聆听 promise 的成功完成完全是可选的。 有时有理由不在乎。 例如,我经常不让 function 等待 function 末尾的文件关闭。
忽略被拒绝的 promise 是一个问题,因为这类似于同步代码中的未处理异常。
因此,只要您正在处理所有可能的错误,就像您使用somePostProcessing().catch(...)
一样,那么忽略完成就没有问题。
注意:您确实需要确保您的代码预测到如果await doCreateOrder()
中出现错误会发生什么,因为这将立即拒绝 createOrder() 返回的 promise 因此 createOrder createOrder()
createOrder()
的调用者需要有一个处理程序来处理被拒绝的promise 或者您需要在此 function 中使用try/catch
来捕获和处理可能的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.