Javascript 如何等到函数结束后继续代码节点js

Javascript 如何等到函数结束后继续代码节点js,javascript,node.js,captcha,webautomation,Javascript,Node.js,Captcha,Webautomation,所以我有一个验证码收割机,我手动解决验证码以获得验证码令牌, 所以我想做的是等待我完成验证码的求解,获取令牌,发送令牌并调用函数来完成签出,这里发生的事情是在我完成验证码的求解之前调用函数,例如在代码中(不会放入真正的代码,因为它非常长) SolvingCaptcha=函数(){ token=“1234” 令牌\u列表。推送(令牌) } 最终令牌=令牌列表[0] //木偶演员代码。 等待解决验证码(); document.getElementById(“g-recaptcha-response”

所以我有一个验证码收割机,我手动解决验证码以获得验证码令牌, 所以我想做的是等待我完成验证码的求解,获取令牌,发送令牌并调用函数来完成签出,这里发生的事情是在我完成验证码的求解之前调用函数,例如在代码中(不会放入真正的代码,因为它非常长)

SolvingCaptcha=函数(){
token=“1234”
令牌\u列表。推送(令牌)
}
最终令牌=令牌列表[0]
//木偶演员代码。
等待解决验证码();
document.getElementById(“g-recaptcha-response”).innerHTML={
最终代币
}
checkoutJsonCall();
长话短说,我希望在继续执行下一个函数之前声明token变量

验证码解算完成后要执行的函数 提前感谢您,并对缺乏解释技巧表示歉意:p

编辑:这里需要说明的是,这是一个非常时间敏感的操作,所以我无法进行睡眠或等待或任何操作,我只需要它在完成后执行即可。谢谢


编辑2:代码等待验证码收割机加载验证码,但不要等到我解决它。

您可以使用promise作为解决验证码的包装,一旦用户指出它已经解决了验证码,或者我猜您必须有某种方式知道用户已经解决了验证码,所以一旦您知道它,调用resolve callback以执行稍后的代码

const SolvingCaptcha = function() {
   return new Promise((resolve, reject) => { 
   //solving captcha 
      if(captchaSolvedEvent) {
          token = "1234"
          token_list.push(token)
          resolve(); // this is the important part here, we are resolving means our work is done i.e captcha is solved, no need to call this until user solves the capcha
      }
   }
}

async function yourMainFunction() {
    await SolvingCaptcha();
    final_token=token_list[0]
    //puppeteer code.
    document.getElementById("g-recaptcha-response").innerHTML={final_token}
    checkoutJsonCall();
}

请注意,如果使用wait,则必须将函数标记为async,在此等待表明,在solvingCapcha函数解析promise之前,它下面的任何代码都不会作为回调传递到js堆栈中执行

您可以使用promise作为solvingCaptcha的包装器,一旦用户指出它已经解决了capcha,或者我猜您一定有某种方式知道用户已经解决了capcha一旦您知道了它,调用resolve callback以执行稍后的代码

const SolvingCaptcha = function() {
   return new Promise((resolve, reject) => { 
   //solving captcha 
      if(captchaSolvedEvent) {
          token = "1234"
          token_list.push(token)
          resolve(); // this is the important part here, we are resolving means our work is done i.e captcha is solved, no need to call this until user solves the capcha
      }
   }
}

async function yourMainFunction() {
    await SolvingCaptcha();
    final_token=token_list[0]
    //puppeteer code.
    document.getElementById("g-recaptcha-response").innerHTML={final_token}
    checkoutJsonCall();
}

请注意,如果使用wait,则必须将函数标记为async,此处wait表示在solvingCapcha函数解析promise之前,函数下面的任何代码都不会作为回调传递到js堆栈中执行,您可以执行while循环,检查变量是否已定义,如果尚未定义,则设置超时。

const SolvingCaptcha = function() {
 
function waitForElement(){
    tokenn = TOKEN_LIST[0]

    console.log(tokenn)
   while(typeof tokenn !== "undefined"){
        //variable exists, do what you want
    
        setTimeout(waitForElement,1500);//so this refreash every 1.5sec to check if to recheck if the tokenn is defined or yer 
    }
}
 waitForElement();
  }


const function2 = function(){

solvingcaptcha();

//what ever you want to do next 
}

希望这能有所帮助。

您好,您可以执行while循环,检查变量是否已定义,如果尚未定义,则设置超时。

const SolvingCaptcha = function() {
 
function waitForElement(){
    tokenn = TOKEN_LIST[0]

    console.log(tokenn)
   while(typeof tokenn !== "undefined"){
        //variable exists, do what you want
    
        setTimeout(waitForElement,1500);//so this refreash every 1.5sec to check if to recheck if the tokenn is defined or yer 
    }
}
 waitForElement();
  }


const function2 = function(){

solvingcaptcha();

//what ever you want to do next 
}

希望这有帮助

这能回答您的问题吗?这回答了你的问题吗?