Append multiple rows to spreadsheet

I want to append multiple rows to a spreadsheet data is coming from a multi-selector and I'm using an additional scope that works, but when data is appended it seems to go to fast and the spreadsheet overrides data. I want to add a delay but I haven't figure out how to do it.

This is the code that is iterating:

var companies = selectMultipleCompanies.data.map(row => row.business_name)
companies.forEach(function(business_name) {
         AppendFromMultipleSelector.trigger({
    additionalScope: {
      'additionalScopeBusinessName': business_name
    }
         })   
        })

Hi @eduardogarza,

Thanks so much for your question! This seems like a classic case of asynchronous function calls being difficult to work with in Javascript... I've been there before too!

In this case, I'm assuming that AppendFromMultipleSelector.trigger() is an asynchronous function that you want to fire across every company in selectMultipleCompanies.data.

In that case, you could try using async... await syntax in Javascript. Watch out when using forEach, which is a very annoying exception.

You could try something like this to make sure that data is appended in order correctly:

for (const { business_name } of selectMultipleCompanies.data) {
await AppendFromMultipleSelector.trigger({
additionalScope: {
additionalScopeBusinessName: business_name
}
})
};

Grace

Thanks, @grace :grinning: