javascript - 为什么javascript方法最后执行?

标签 javascript node.js asynchronous async-await puppeteer

我正在使用 puppeteer 和 https://www.deathbycaptcha.com/自动解决验证码。
这是我解决验证码的函数。

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

我正在像这样执行这个函数

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

这是我得到的输出

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

我很困惑为什么在 4 之后它会跳过 client.decode block ?另外,即使我已经明确定义了它,为什么我还是会收到 captchaText is not Defined ?以及如何正确执行?

最佳答案

asyncawait 关键字是管理 promise 的工具。

client.decode 接受回调(您没有对其调用 then()),因此它可能不会返回 promise 。

因此 await 关键字不起作用。

您可以围绕回调函数创建一个 promise 。

进一步阅读:

关于javascript - 为什么javascript方法最后执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66060826/

相关文章:

JavaScript 和使用 MVC 风格模型(带 Angular )

javascript - 当我提前知道要着色的行数和表格长度时,如何交替表格中的多行颜色

javascript - 在 node.js 中使用多个异步 redis 调用正确放置回调

javascript - 如何从生成器函数调用的异步回调中产生 yield ?

javascript - 在文本框上输入时如何避免 css 和脚本效果

javascript - 修改 javascript var 值而不重新加载页面

javascript - 了解模块的 Javascript 作用域

javascript - 如何将图像数据从node.js传输到html5 canvas?

node.js - 如何使用 MongoDB、Node.js(+express) 和 Jade 创建 MVC 表单

javascript - Await 仅在 nodejs 的异步函数中有效