Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

node.js - Why does javascript method executes last?

I'm using puppeteer and https://www.deathbycaptcha.com/ to automatically solve a captcha.
Here is my function to solve the captcha.

async function solve_captcha(page, client, captcha_selector, imgName) {
  console.log("0")
  const attr = await page.$$eval(captcha_selector, el => el.map(x => x.getAttribute("style")));
  console.log("1")
  const captchaUrl = attr[0].match(/(?<=url(')(.*)(?='))/g)[0];
  console.log("2")
  await base64ImageDecoder.convert(captchaUrl, './', imgName).then(result => {console.log(result);}).catch(err => console.error(err));
  console.log("3")
  const captcha_file = imgName+'.jpg';
  console.log("4" + captcha_file)
  await client.get_balance((balance) => {
    console.log(balance);
  });
  let captchaText = "abc";
  await client.decode({captcha: captcha_file, extra: {type: 0}}, (captcha) => {
    if (captcha) {
      console.log('Captcha ' + captcha['captcha'] + ' solved: ' + captcha['text']);
      console.log("5");
      captchaText = captcha['text'];
      console.log("6");
    };
  });
  console.log("7");
  await page.$eval('#captchaField', el => el.value = captchaText);
  console.log("8");
  await page.click('#submitbtn');
  console.log("9");
}

I'm executing this function like this

(async () => {
// some puppeteer code
await solve_captcha(page, client, 'captcha > div', 'captcha');
// some puppeteer code
})();

Here is the output I'm getting

0
1
2
{"name":"captcha.jpg","type":"jpg","path":"./","fullPath":"./\captcha.jpg"}
3
4captcha.jpg
7
(node:15000) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: captchaText is not defined
(node:15000) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15000) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 
690.3853
Captcha 221820197 solved: XZ4xS9Lsw
5
6

I'm confused why after 4 it skips the client.decode block? Also why am I getting captchaText is not defined even when I've clearly defined it? And how to execute it correctly?

question from:https://stackoverflow.com/questions/66060826/why-does-javascript-method-executes-last

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The async and await keywords are tools to manage promises.

client.decode takes a callback (you aren't calling then() on it) so it, presumably, doesn't return a promise.

The await keyword thus has no effect.

You can create a promise around your callback function.

Further reading:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...