I have an asynchronous function in Node.js I am calling over and over again. It basically fetches some data from API, processes it and this repeats forever. I have wrapped the whole function inside a try catch block with a finally statement, which calls the function again.
It looks like this:
async function infiniteLoop()
{
try
{
console.log(Date.now())
const response = await axios.get(...) // fetch data
await Promise.all(response.data.entities.map(async (entity) =>
{
const product = new Product(entity)
await product.resolve()
}))
}
catch (e)
{
console.error(e)
}
finally
{
setTimeout(() => infiniteLoop(), 1000)
}
}
The thing is that this infinite loop is sometimes broken and I have no reason why. By breaking I mean that the function is no longer doing anything. I was thinking it may be stuck somewhere, but I am using only axios (with timeout set to 5000 ms) and prisma. Nothing is printed out to console in the catch block etc. The Node process does not ever crash if the infinite loop breaks.
The infinite loop breaks randomly. Sometimes it breaks after several hours, sometimes after few days.