简体   繁体   中英

Nodejs any problem in calling async function without await?

I have an endpoint where I want to do some async post processing in which user is not interested in, so I want to process them asynchronously and send send the response the user.

something like this.

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)

}

But I have a concern that would it cause any issues like memory leak(my team lead says so) and so on? also what is the best way or other options to do such post processing in nodeJS?

It is not a problem to ignore the result of a promise. It's totally optional whether you listen for the successful completion of a promise or not. There are sometimes reasons to not care. For example, I often don't make a function wait for the closing of a file at the end of the function.

It is a problem to ignore a rejected promise as that's analogous to an unhandled exception in synchronous code.

So, as long as you are handling all possible errors which it looks like you are with somePostProcessing().catch(...) , then it's no problem to ignore the completion.

Note: you do need to make sure your code is anticipating what happens if there's an error in await doCreateOrder() as that will immediately reject the promise returned by createOrder() so the caller of createOrder() needs to have a handler for that rejected promise OR you need a try/catch inside this function to catch and handle that possible error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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