0

This code worked until I put it in a ForEach loop. The issue appears to be with the 'await fetch(integromat_webhook_url + aj);' line as it throws this error 'await is only valid in async function'.

I'm trying to send multiple webhooks to integromat.

Is there a way to do without the AWAIT part or make it an ASYNC Function please?

I'm a noob and just learning javascript :-).

Thanks

Jonathan

console.log('Filtered PIDs:', filteredPids);

let worksheetsCreated = filteredPids.length;
let integromat_webhook_url = "";

if(worksheetsCreated > 0){
    
    output.markdown(worksheetsCreated + " worksheets created and being sent to indiviudal groups.");

    //ADD FILTERED PRODUCTION WORKSHEETS TO TABLE
    let recordsCreated = await batchAnd('Create', groupworksheetsBase, filteredPids);

    //GET ARRAY OF GROUPS IN FILTERED PRODUCTION WORKSHEET
    let unique = [...new Set(filteredPids.map(item => item.fields.Group))];
    console.log('unique groups in filtered PIDs',unique);    

    //LOOP THROUGH UNIQUE GROUPS
   
    unique.forEach(function(uGroup) {
        integromatArray = filteredPids.filter(pid => pid.fields.Group == uGroup)
        console.log(uGroup, integromatArray);
        switch(uGroup) {
          case 'Birkenhead':
          integromat_webhook_url = "https://hook.integromat.com/mksobdvdxxxxxxxxxxx?pidsArray=";
          break;
          case 'Taupo':
          integromat_webhook_url = "https://hook.integromat.com/9c6y4279kxxxxxxxxxx?pidsArray=";
          break;
        }
        const aj = JSON.stringify(integromatArray);
        console.log('stringify array',aj);
        await fetch(integromat_webhook_url + aj);

    }); 

 
} else {
    output.markdown("No new worksheets to add.");
}

Thanks so much

Jonathan

Jonathan Lyon
  • 3,862
  • 7
  • 39
  • 52

3 Answers3

0

just add async before the function keyword

unique.forEach(async function(uGroup) {
        integromatArray = filteredPids.filter(pid => pid.fields.Group == uGroup)
        console.log(uGroup, integromatArray);
        switch(uGroup) {
          case 'Birkenhead':
          integromat_webhook_url = "https://hook.integromat.com/mksobdvdxxxxxxxxxxx?pidsArray=";
          break;
          case 'Taupo':
          integromat_webhook_url = "https://hook.integromat.com/9c6y4279kxxxxxxxxxx?pidsArray=";
          break;
        }
        const aj = JSON.stringify(integromatArray);
        console.log('stringify array',aj);
        await fetch(integromat_webhook_url + aj);

    }); 
Moukim hfaiedh
  • 409
  • 6
  • 9
  • using async/await in a forEach loop rarely works as expected - e.g. all those `fetch` will run concurrently, not in sequence – Jaromanda X Sep 23 '20 at 22:02
  • yes you are tight i found this answered question https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop this may help you – Moukim hfaiedh Sep 23 '20 at 22:10
0

I used this approach to get it working as suggested by a friend of mine...

console.log('Filtered PIDs:', filteredPids);

let worksheetsCreated = filteredPids.length;
let integromat_webhook_url = "";

if(worksheetsCreated > 0){
    
    output.markdown(worksheetsCreated + " worksheets created and being sent to indiviudal groups.");

    //ADD FILTERED PRODUCTION WORKSHEETS TO TABLE
    let recordsCreated = await batchAnd('Create', groupworksheetsBase, filteredPids);

//GET ARRAY OF GROUPS IN FILTERED PRODUCTION WORKSHEET
    const unique = [...new Set(filteredPids.map(item => item.fields.Group))];
    console.log('unique groups in filtered PIDs', unique);

    //LOOP THROUGH UNIQUE GROUPS
    await Promise.all(
        unique.map(async uGroup => {
            integromatArray = recordsArray.filter(pid => pid.fields.Group == uGroup);
            console.log(uGroup, integromatArray);
            switch (uGroup) {
                case 'Birkenhead':
                    integromat_webhook_url = 'https://hook.integromat.com/mksobdvdu8uydiq9x22mxptgaye6ueji?pidsArray=';
                    break;
                case 'Taupo':
                    integromat_webhook_url = 'https://hook.integromat.com/9c6y4279kydmqm7hswjutwhp7fu814aa?pidsArray=';
                    break;
            }
            const aj = JSON.stringify(integromatArray);
            console.log('stringify array', aj);
            console.log('url',integromat_webhook_url + aj);
            const result = await fetch(integromat_webhook_url + aj);
            return result;
        })
    );

 
} else {
    output.markdown("No new worksheets to add.");
}
Jonathan Lyon
  • 3,862
  • 7
  • 39
  • 52
0

Change your forEach loop to a for loop and do not forget to put the async keyword before the function keyword like :

async function test() {
}
Beweelam
  • 870
  • 9
  • 11