S
S
Sp1keazyYT2019-04-14 22:23:57
JavaScript
Sp1keazyYT, 2019-04-14 22:23:57

How to transfer the received value of a variable from one async function to another function in Node JS?

Good evening. There is some anticaptcha.getBalance function:

var anticaptcha = require('./anticaptcha')('***'), image2base64 = require('image-to-base64');

anticaptcha.getBalance(async function (err, balance) {
    if (err) {
        console.error(err);
        return;
    }

    anticaptcha.setMinLength(5);

  const API_URL = 'https://api.vk.com/captcha.php?sid=159568716370&s=1';
  const body = await image2base64(API_URL).catch((error) => console.log(error));
  
    if (balance > 0) {
        anticaptcha.createImageToTextTask({
                case: true, // or params can be set for every captcha specially
                body
            },
            function (err, taskId) {
                if (err) {
                    console.error(err);
                    return;
                }

                console.log(taskId);

                anticaptcha.getTaskSolution(taskId, function (err, taskSolution) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    console.log(taskSolution);
                });
            }
        );
    }
});

And also a piece of another function:
// Init app
(async _=> {
  setUTitle("Loading app");

  if(!VK_TOKEN) {
    let succ = await initToken();
    if(!succ) { process.exit(); }
  }
  vk.token = VK_TOKEN;

  vk.captchaHandler = async ({ src, type }, retry)=> {
    let key = await rl.questionAsync("Введи капчу ["+src+"]: ");

    try {
      await retry(key);
      con('Всё ок.');
    } catch (e) { con("Всё фигово. "+e.message, true); }
  };
})();

The task is as follows: from the first anticaptcha.getBalance function, transfer the obtained value of the taskSolution constant on the 33rd line of the code to the second function, in which the vk.captchaHandler function is also nested. There is a src variable in the vk.captchaHandler function - this is a link to the image. The logic of this function is as follows: a link to the image is sent to the src variable. A message is displayed in the console asking a person to enter a captcha and a link to the captcha image is displayed in brackets. Then there is a loop waiting for the input of this captcha, if the captcha is correct, then the message "Everything is OK" is displayed. It is necessary to make sure that a person does not have to manually enter something, the value with captcha should already come from the first function in the taskSolution variable and it turns out to be substituted into the key variable (in the vk.captchaHandler function). I hope I explained more or less clearly. Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-04-14
@Sp1keazyYT

let getTaskSolution = (src) => {
  return new Promise((resolve, reject) => {
    anticaptcha.getBalance(async (err, balance) => { 
      if (err) {
        reject(err); 
        return;
      }

      anticaptcha.setMinLength(5);
      if (balance > 0) {
        anticaptcha.createImageToTextTask({
          case: true, // or params can be set for every captcha specially
          body: await image2base64(src).catch(reject)
        }, (err, taskId) => {
          if (err) {
            reject(err);
            return;
          }

          anticaptcha.getTaskSolution(taskId, (err, taskSolution) => {
            if (err) {
              reject(err);
              return;
            }

            resolve(taskSolution)
          });
        });
      }
    });
  });
};


(async () => {
  setUTitle("Loading app");

  if (!VK_TOKEN) {
    let succ = await initToken();
    if (!succ) { 
      process.exit(); 
    }
  }
  vk.token = VK_TOKEN;

  vk.captchaHandler = async ({src, type}, retry) => {
    let key = await getTaskSolution(src).catch((error) => console.log(error));
    try {
       await retry(key);
       con('Всё ок.');
    } catch (e) { 
      con("Всё фигово. " + e.message, true); 
    }
  };
})();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question