Currently, I am developing an API that requires making repeated calls to another API (specifically, Quickbooks Online) within a loop. These calls are encapsulated in promises that either resolve or reject based on the response from Quickbooks. Everything functions as expected, but I am looking for a way to allow the loop to continue execution after handling errors. At present, when an error is caught, it gets logged into the database, but the loop halts and no further calls are executed. I have attempted using "return" and "continue" inside the catch block without success.
Below is an example of the code:
// This is a section of a larger loop. Needs to be asynchronous
await paymentInfo.forEach(async ln => {
var payRef = ln.payment_ref
var vendor = ln.vendor_id
var finalPayment = await service.createPayment(args)
.then(console.log("Payment created"))
.catch((err) => {
// Log the error to the database
service.dbUpdate(err)
.then(console.log("Error Logged to DB"))
.catch(() => {})
// How do we proceed from here?
})
// Update the database with payment ID here
})
In essence, I simply want to be able to continue to the next iteration of the loop after catching an error. As far as I can see, there isn't a straightforward way to achieve this. Any suggestions would be greatly appreciated.