Custom app | Code doesn't continue after a foreach loop

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?

Thanks!

Hi @Matheus_Souza_Silva!

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

2 Likes

When I add await in the code of onDealCreateHandler and executes the fdk pack command for deployment, it shows this error:

Do I’ve add some export to use async functions here?

In every function you use await, you need to specify it as asynchronous, using async before declaration, as follows in the code:

async function anyFuntion(param) {
  try {
    const resp = await client.request.get('https://someULR.com');
    console.log(resp);
  } catch(err) {
    console.log(err);
  }
}

regards

Felipi Lima Matozinho

1 Like

I forgot to mention that I already did this.

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…

1 Like

Can you share the code around line 96?

Re: the loop — like @Matozinho said, I recommend using Promise.all() for a scenario like yours. You can also use for..of loop instead:

// inside an async function
for (const product_id of array_products) {
  try {
    const data = await $request.get(url, options);
    // Remaining logic
  } catch (err) {
    // Error handling logic
}

This will make sure the loop waits for each $request call to complete before moving on to the next iteration.

2 Likes

Ok, so I think I found what the issue was, the Promise.all was inside of another promise, like so:

onDealCreateHandler: async function (args) {
   await $request.get(url, header)
   .then(
     function(data){
       let array= JSON.parse(data.response.deal.products);

       await Promise.all(array.map(async (product_id) => {
         //logic
       });
   })
}

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.

So I’ll find another way to do what I need.

Thanks.

If you are using await in expression you don’t need to use then in the promise

You can do:

onDealCreateHandler: async function (args) {
   try {
      const { response } = await $request.get(url, header);

      //destructuring object
      const { deal: { products } } = JSON.parse(response);

      //this will make all requests in parallel
      await Promise.all(products.map(async (product_id) => {
         //logic
      }));
   } catch (err) {
      console.log(err);
   }  
}

You may try this code snippet to validate the idea, if you want.

Regards.

1 Like

I didn’t know that, thanks for the help!

vlw.

1 Like