0

so i have a captcha harvester that i solve captcha manually to obtain the token of the captcha, so what i want to do is to wait till I finish solving the captcha and get the token and send the token and call a function to finish the checkout, what happening here is the functions are being called before i finish solving the captcha for example in code(will not put the real code since it's really long)

SolvingCaptcha = function() {
  token = "1234"
  token_list.push(token)
}

final_token = token_list[0]

//puppeteer code.
await SolvingCaptcha();
document.getElementById("g-recaptcha-response").innerHTML = {
  final_token
}

checkoutJsonCall();

long story short I want the token variable to be declared before it continues to the next functions

the function that I want to execute after the captcha solving is completed thank u in advance and sorry for the lack of skills in explanation :p

EDIT: to be clear here it's a very time-sensitive operation so I cant do sleep or wait or anything I just need it to execute exactly after it finished.Thank u

EDIT 2 : the code wait till the captcha harvester to load the captcha, but don't wait until i solve it.

  • 1
    Does this answer your question? [Proper way to wait for one function to finish before continuing?](https://stackoverflow.com/questions/21518381/proper-way-to-wait-for-one-function-to-finish-before-continuing) – Zac Anger Jan 19 '21 at 18:19

2 Answers2

3

Hello,u can just do while loop that check the variable if its defined or yet and set a timeout if it didnt.

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 
}

hope this helps out

Taha Daboussi
  • 350
  • 3
  • 14
1

You can use promise as a wrapper for your solvingCaptcha and once user indicate that it has solved the capcha or I guess you must have some way of knowing that user has solved the capcha so once you know it, call resolve callback to execute later code

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

note that you have to make your function marked with async if you are using await , here await indicate that no code below it will passed as callback to be executed in js stack until promise is resolved from our solvingCapcha function

mss
  • 1,423
  • 2
  • 9
  • 18