简体   繁体   English

未捕获的 SyntaxError:await 仅在异步函数中有效(尽管它在异步函数中......)

[英]Uncaught SyntaxError: await is only valid in async function (its in an async function though…)

So I have no clue what's causing this issue and I've spent at least 30 minutes searching google and trying different things with no solution.所以我不知道是什么导致了这个问题,我花了至少 30 分钟搜索谷歌并尝试不同的东西,但没有解决方案。 I've defined an async function and I'm trying to use await inside of it but its giving me the我已经定义了一个async函数,我试图在它里面使用await但它给了我

Error: Uncaught SyntaxError: await is only valid in async function错误:未捕获的语法错误:等待仅在异步函数中有效

Here is the code:这是代码:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
async function SolveRecaptchaV2(APIKey, googleKey, pageUrl, proxy, proxyType){
            var requestUrl = "https://2captcha.com/in.php?key=" + APIKey + "&method=userrecaptcha&googlekey=" + googleKey + "&pageurl=" + pageUrl + "&proxy=" + proxy + "&proxytype=";

            switch (proxyType) {
                case 'HTTP':
                requestUrl = requestUrl + "HTTP";
                break;

                case 'HTTPS':
                requestUrl = requestUrl + "HTTPS";
                break;

                case 'SOCKS4':
                requestUrl = requestUrl + "SOCKS4";
                break;

                case 'SOCKS5':
                requestUrl = requestUrl + "SOCKS5";
                break;
            }   
            $.ajax({url: requestUrl, success: function(result){
                if(result.length < 3){
                    return false;
                }else{
                    if(result.substring(0, 3) == "OK|"){
                        var captchaID = result.substring(3);

                        for(var i=0; i<24; i++){
                            var ansUrl = "https://2captcha.com/res.php?key=" + APIKey + "&action=get&id=" + captchaID;  

                            $.ajax({url: ansUrl, success: function(ansresult){
                                    console.log(ansresult);
                                    if(ansresult.length < 3){
                                        return ansresult;
                                    }else{
                                        if(ansresult.substring(0, 3) == "OK|"){
                                            return ansresult;
                                        }else if (ansresult != "CAPCHA_NOT_READY"){
                                            return ansresult;
                                        }
                                    }
                                }
                            });
                            await sleep(1000);
                        }

                    }else{
                        return ansresult;   
                    }
                }
            },
            fail: function(){
                return "";
                }
            });

        }

EDIT:: Now, when i make the $.ajax callback function an async function, neither of the $.ajax calls work, and any console.log's i make inside of them dont show in the console... i dont get any errors though编辑:: 现在,当我将 $.ajax 回调函数设为异步函数时,$.ajax 调用都不起作用,并且我在其中创建的任何 console.log 都不会显示在控制台中......我没有收到任何错误虽然

So the main issue here is, you're using await within the success function from $.ajax function (This is not possible, without making this function async as well, but now because your code is mixing callbacks/async, you're not able to resolve the original Promise of SolveRecaptchaV2 . You're mixing async with old callbacks.) Your code is essentially doing:所以这里的主要问题是,你在$.ajax函数的成功函数中使用 await(这是不可能的,如果不使这个函数也异步,但现在因为你的代码混合了回调/异步,你不是能够解决SolveRecaptchaV2的原始承诺。您将异步与旧回调混合在一起。)您的代码本质上是在做:

SolveRecaptchaV2 => Ajax Request => Ajax Request Done, Firing callback
                         V                         V
                     Returning         Returning (But nothing cares about this return)

If you're using async functions, try to utilize async as much as possible.如果您使用 async 函数,请尝试尽可能多地使用 async。 $.ajax will return a promise, so we can await that, and keep our function completely async: $.ajax将返回一个 promise,因此我们可以等待它,并保持我们的函数完全异步:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function SolveRecaptchaV2(APIKey, googleKey, pageUrl, proxy, proxyType){
    var requestUrl = "https://2captcha.com/in.php?key=" + APIKey + "&method=userrecaptcha&googlekey=" + googleKey + "&pageurl=" + pageUrl + "&proxy=" + proxy + "&proxytype=";

    switch (proxyType) {
        case 'HTTP':
        requestUrl = requestUrl + "HTTP";
        break;

        case 'HTTPS':
        requestUrl = requestUrl + "HTTPS";
        break;

        case 'SOCKS4':
        requestUrl = requestUrl + "SOCKS4";
        break;

        case 'SOCKS5':
        requestUrl = requestUrl + "SOCKS5";
        break;
    }   
    try {
        await result = await $.ajax({url: requestUrl});

        if(result.length < 3) {
            return false;
        } else {
            if(result.substring(0, 3) == "OK|") {
                var captchaID = result.substring(3);

                for(var i=0; i<24; i++){
                    var ansUrl = "https://2captcha.com/res.php?key=" + APIKey + "&action=get&id=" + captchaID;  

                    var ansResult = await $.ajax({url: ansUrl});
                    console.log(ansResult);
                    if(ansResult.length < 3) {
                        return ansResult;
                    } else {
                        if(ansresult.substring(0, 3) == "OK|") {
                            return ansResult;
                        } else if (ansResult != "CAPCHA_NOT_READY") {
                            return ansResult;
                        }
                    }
                    await sleep(1000);
                }
            } else {
                // ansResult is not defined here, not sure what you want to return here (May want to return false, or an empty string):
                return ansResult;   
            }
        }
    } catch (err) {
        //On ajax failure, return empy string. (May want to return false here, to fall inline with your "if (result.length < 3)" statement above.)
        return "";
    }
}

Now our chain looks like this:现在我们的链看起来像这样:

SolveRecaptchaV2 => Ajax Request
                     => Ajax Request Done
                       => Get Answer URL
                         => Sleep
                           => Get Answer URL
                             => Sleep (...loop)
                               => Resolve promise of SolveRecaptchaV2.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的SyntaxError:await仅在异步函数的异步函数中有效 - Uncaught SyntaxError: await is only valid in async function in async function 未捕获的 SyntaxError:await 仅在异步函数中有效(FireBase 文档) - Uncaught SyntaxError: await is only valid in async function (FireBase documentation) Javascript:SyntaxError:await仅在异步函数中有效 - Javascript: SyntaxError: await is only valid in async function javascript await function in script 返回 Uncaught SyntaxError: await is only valid in async functions and the top level body of modules - javascript await function in script returns Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules SyntaxError: await 仅在异步中有效 function **而且我有异步** - SyntaxError: await is only valid in async function **and I have async** meteo.js:21 Uncaught SyntaxError:await仅在异步函数中有效 - meteo.js:21 Uncaught SyntaxError: await is only valid in async function SyntaxError: await 仅在 async 函数中有效。 无法纠正 - SyntaxError: await is only valid in async function. Unable to Correct it Appium 代码生成“SyntaxError: await is only valid in async function” - Appium code generates "SyntaxError: await is only valid in async function" Node Js - SyntaxError: await 仅在异步中有效 function - Node Js - SyntaxError: await is only valid in async function SyntaxError: await 仅在我的代码中的异步 function 中有效 - SyntaxError: await is only valid in async function in my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM