How to use await in Promises?

Hello everyone,
Thanks to the help of @victoria I'm using this JS function to send multiple queries to Zoho CRM API but unfortunately, this is too much for the API, I have a too many request in the console.

const arr = table15.data
const query = get_did_pagex7

const promises = arr.map((item) => {
    return query.trigger({
      additionalScope: {
      criteria4 : item.Dealer_s_Id_associ_s.id
      }
   });
});

return Promise.all(promises)

So I'm wondering if I could add an await function inside this function let the API get some rest before sending the next query.

What do you think ?

Thanks

try the following
await query.trigger({ additionalScope: {criteria4 : item.Dealer_s_Id_associ_s.id})

1 Like

Thanks a lot for this. I tried this

const arr = table15.data
const query = get_did_pagex7

const promises = arr.map((item) => {
  
  await query.trigger({ 
    additionalScope: {
      criteria4 : item.Dealer_s_Id_associ_s.id
    }
  });
                       

});

return Promise.all(promises)

And get this answer in popup notification
did: await is only valid in async functions and the top level bodies of modules

Hey @rolandapercur, you would need to wrap your script in an async function where await is permitted.

Try this:

(async () => {
  var array = table15.data;

  for (let i = 0; i < array.length; i++) {
    await new Promise((resolve) => {
      get_did_pagex7.trigger({
        additionalScope: {
          criteria4: array[i].Dealer_s_Id_associ_s.id
        },
        onSuccess: (data) => {
          resolve(data);
        }
      })
    });
  };
})();

Does this work for you?

1 Like

Hello Minijohn,

Thanks a lot for your answer. Unfortunately, even if it looks to work Query ran successfully , it returns nothing.
I tried to add

return Promise.all(promises)

or

return Promise.all()

But it doesn't work.

It shouldn't return anything (just true as it passes and triggers the notification).

Does it not call the API correctly for your entries or do you need to take further action based on the response of the API call?

It looks that the API is correclty called - so this is a first good step. But in the meantime, I would like to display the result which should be an array of objects.
Then I need to flatten the array of object to get only a single array.
I did it with a Transformer return data.map(obj => obj.data['0'])
but for the moment, as I returned nothing, it is not exactly what I'm looking for.
In addition, I don't have any data type for the query when it ends. It is undefined