【问题标题】:How to fix Await fails in loop with Async error如何修复 Await 在循环中失败并出现异步错误
【发布时间】:2020-09-23 21:44:35
【问题描述】:

此代码一直有效,直到我将其放入 ForEach 循环中。问题似乎与 'await fetch(integromat_webhook_url + aj);' 行有关,因为它会引发此错误 'await is only valid in async function'。

我正在尝试将多个 webhook 发送到 integromat。

有没有办法不使用 AWAIT 部分或将其设为 ASYNC 函数?

我是个菜鸟,刚刚学习 javascript :-)。

谢谢

乔纳森

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.");
}

非常感谢

乔纳森

【问题讨论】:

    标签: javascript async-await


    【解决方案1】:

    只需在 function 关键字前添加 async

    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);
    
        }); 
    

    【讨论】:

    • 在 forEach 循环中使用 async/await 很少能按预期工作 - 例如所有fetch 将同时运行,而不是按顺序运行
    • 是的,你很紧张,我发现这个已回答的问题stackoverflow.com/questions/37576685/… 这可能对你有帮助
    【解决方案2】:

    我按照我朋友的建议使用了这种方法来让它工作......

    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.");
    }
    

    【讨论】:

      【解决方案3】:

      将你的 forEach 循环更改为 for 循环,不要忘记将 async 关键字放在 function 关键字之前,例如:

      async function test() {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-15
        • 2019-08-23
        • 2019-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多