I am working on a project which is built using express and node js. I just need to call the async function inside the SetTimeout function (callback)
Here is my Controller File
module.exports.create = async (req, res) => {
try {
// Some DB Calls
setTimeout(() => updateWhOrders(req.Orders, req), 0)
// Some DB Calls
return res.status(200).json({ data , msg: "record created successfully", success: true })
} catch (error) {
if (error?.message?.includes("Validation error")) {
return handleErr({ message: "This supplier order id has already been added. Please refresh the page and check further down the screen." }, res)
}
return handleErr(error, res)
}
}
Here is the async function in the same controller
const updateWhOrders = async (allOrders, req) => {
// Some DB Calls using async await
await SourceOrder.bulkCreate(allOrders.data, { updateOnDuplicate: ["wh_address"] })
}
Now I want to ask the difference between these two statements
1. setTimeout(() => updateWhOrders(req.Orders, req), 0)
2. updateWhOrders(req.Orders, req)
I just want to call the updateWhOrders function parallel before sending the response back to the API.
Is there any specific reason to use the setTimeout function? Or if I omit the setTimeout function it will behave exactly the same as using setTimeout function?
According to my understanding if I omit setTimeout function it will run in the background by returning a promise. What if I call function(updateWHOrders) inside setTimeout function. What is the benefit? Please correct me if I am wrong.
Thank you in Advance :)