I have an endpoint on my server named "/order".
When it gets triggered, I need to send an order over to an API. Sometimes, because some data over ipfs takes too long to download, the order fails within the try-catch block, and I need to resend it.
Since a while loop that tries to resend the order until it is successful would be blocking other requests, I thought I could make one myself using recursion and a setTimeout, to try to send the same request, say, 3 times every 5 minutes and if the third time it fails then it won't try again.
I wanted to know if this was the right way to achieve the non-blocking functionality and if there are some vulnerabilities/things I'm not taking into account:
async function makeOrder(body, tries) {
if (tries > 0) {
let order;
try {
order = getOrderTemplate(body.shipping_address)
for (const index in body.line_items) {
order.items.push(await getItemTemplate(body.line_items[index]))
}
await sendOrderToAPI(order)
} catch (err) {
setTimeout(function(){
makeOrder(body, tries - 1)
}, 180000)
}
} else {
console.log("order n " + body.order_number + " failed")
//write failed order number to database to deal with it later on
}
}