Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在异步等待中使用循环提供eslint错误_Javascript_Node.js_Performance_Async Await_Eslint - Fatal编程技术网

Javascript 在异步等待中使用循环提供eslint错误

Javascript 在异步等待中使用循环提供eslint错误,javascript,node.js,performance,async-await,eslint,Javascript,Node.js,Performance,Async Await,Eslint,我有下面的代码,它可以工作,我当前配置了eslint,并得到以下错误 1. ESLint: iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations. (no-restricted-syntax) 2. ESLint

我有下面的代码,它可以工作,我当前配置了eslint,并得到以下错误

1. ESLint: iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations. (no-restricted-syntax)

2. ESLint: Unexpected `await` inside a loop. (no-await-in-loop)
该守则的构思如下:

  • 在包含帐户列表的全局帐户上循环(第一个为)

  • 为欧盟地区准备申请

  • 为美国地区准备申请

  • 运行异步请求以从EU获取用户

  • 在值上循环,如果找到url返回 6-7与4-5相同,但适用于美国地区

     async function acount(global) {
    
         // here i got the first eslint error iterators/generators
         for (const account of global) {
    
              // create http request for getting the users - region EU
             let usersEUReq = getUsers(account.guid, region.1);
    
             // create http request for getting the users region US
             let usersUSReq = getUsers(account.guid, region.2);
    
             const usersEU = await axios(usersEUReq);
    
           // here I got the response from the promise and should loop on it got get URL
             for (const sub of usersEU.data) {
                 if (sub.url) {
                     return sub.url
                 }
             }
    
             const usersUS = await axios(usersUSBReq);
             for (const sub of usersUS.sub) {
                 if (sub.url) {
                     return sub.url
                 }
             }
     }
    

  • 顺便说一句,我不能使用
    Promise.all
    race
    ,因为我需要为欧盟和美国运行代码除非您明确希望连续循环等待的承诺,否则您应该使用
    Promise.all
    Promise.race
    (或
    Promise.any()
    ,当所有主要浏览器都支持它时)。他们会同时履行你的承诺

    例如:

    function getuserdata(account) {
        return [region.1, region.2].map(_region => getUsers(account.guid, _region)).map(axios);
    }
    
    async function acount(global) {
        let userdata = await Promise.all(global.flatMap(getuserdata))
        for (const sub of userdata) {
            if (sub.url) {
                return sub.url
            }
        }
    }
    

    如果您确实想执行串行循环,那么您所拥有的将起作用。

    如果这些linting错误对您来说是个问题,那么为什么要启用
    无限制语法
    无等待循环
    规则?出于某种原因,你有义务使用这些规则吗?@JakeHolzinger-我使用eslint最佳实践,没有做任何特别的事情…,是否有其他方法可以编写此代码,或者干脆忽略这些规则?我不想写bed代码…林廷规则并不难。它们取决于具体情况。如果它们不适合您的情况,请将其关闭。如果您希望您的循环按顺序执行请求,则循环中的no wait规则将不适用。查看它的文档-您需要关闭它,以防它产生所有误报。您可以执行Promise.all,其中每个项都是执行循环体的异步函数。i、 e.尝试编写一个
    foreachasync
    函数,以便可以按任何顺序处理帐户,但EU,then US axios请求会连续发生在每个帐户上。谢谢,我知道这个承诺。我不能在我的情况下使用它,因为我需要先运行一个请求,然后再运行另一个请求,如果我不能使用promise.all和race,那么我必须使用哪个选项才能更好地编写代码?\您给出的第二个选项将如何发出eslint错误?+1作为答案的第一部分,
    .forEach()
    循环不能与
    wait
    一起正常工作,并且不会连续运行它们,因为
    .forEach()
    不支持承诺。您显示的内容甚至不会运行,因为回调必须是
    async
    ,才能使用
    wait
    ,但如果您解决了这个问题,它将运行,但不会连续运行。它将一次运行所有这些。要在循环中使用
    wait
    ,请使用常规的
    for
    循环,而不是
    。forEach
    。谢谢,我刚刚删除了它,因为OP基本上就是这样做的。