Hello there! So I’m developing a Custom app for Freshworks CRM and I’ve the following code:
array_products.forEach(product_id => {
$request.get(`https://domain.myfreshworks.com/crm/sales/api/cpq/products/${product_id}`, headers)
.then(
function (data) {
// Some logic
},
function (error) {
console.error(error);
}
);
});
console.log(variable);
// Rest of code
The problem I’m facing is that after the foreach block is executed, the rest of the code is somehow ignored and doesn’t execute. I’ve checked in the custom app’s logs and like I said, the code soddenly stops. For example, the console.log(variable) part of the code above doesn’t execute along with the rest of the logic.
The logic inside the foreach works fine.
I don’t know if the problem is with the FDK or in Node itself, maybe making several requests in a foreach loop isn’t supported in FDK? What can I do where? Or there is something I’m doing wrong?
you can use Promise.all if you want to wait the response of all requests.
I recommend making the requests inside a async function (so you can use await) and use it inside a try catch block to handle errors, as show in this Stack Overflow Topic
onDealCreateHandler: async function (args) {
//logic
}
This is the function I’m working on, is the onDealCreate trigger of a serverless custom app.
The function is already a asynchronous one, so I should be able to use await.
That’s why I’m very lost here, I read that the FDK 7.0.1 now supports asynchronous functions in the documentation, but it isn’t working for me…
I was doing this to get the data of the array, but this not possible, when I put the Promise.all outside that request, I was able to pack the app on fdk normally.