Return callback variable to main function with Node js

Asked

Viewed 103 times

0

I have a primary function that makes calls from other functions within itself, functions that are called have return with callback as shown in the code below

async function processarFrase(frase) {
  frase = limpezaInicial(frase);
  let fraseOriginal = frase;
  if (frase.match(regexes[0])) {
    let match = await limpeza(frase);
    frase = match;
  }

  getDictionary(frase, dictionary => {
    let grupo = capturarGrupo(fraseOriginal, dictionary);
    console.log(fraseOriginal);
    console.log("Inserir isso no banco, no campo dataset: " + grupo);
    console.log("\n");
  });
}

what needs is that the main function that is the processrFrase returns the variable group because I need to access this variable in another scope, I tried this way below:

async function processarFrase(frase) {
      let group;
      frase = limpezaInicial(frase);
      let fraseOriginal = frase;
      if (frase.match(regexes[0])) {
        let match = await limpeza(frase);
        frase = match;
      }

      getDictionary(frase, dictionary => {
        let grupo = capturarGrupo(fraseOriginal, dictionary);
        console.log(fraseOriginal);
        console.log("Inserir isso no banco, no campo dataset: " + grupo);
        console.log("\n");
        group = grupo;
      });
      return group;
    }

But without success, if I do

 let grupo = processarFrase("Uma frase qualquer");

and give a

console.log(grupo)

the result is

Undefined

how do I so that the main function can return to the group variable?

  • Parentheses are missing between getDictionary parameters. Try this way: getDictionary((frase, dictionary), => { } )

  • I don’t understand very well: you are trying to declare or call the getDictionary function?

  • It does not need parentheses ,and what is needed is for the function to process Rase , returns that variable called group . the group variable is declared inside the getDictionary callback, so I created a variable called group to store what you have in group

  • Ever tried to use await before calling capturarGrupo? post the function code capturarGrupo, please.

  • Yes, I tried but I didn’t succeed

  • 1

    the complete code in the Pastebin https://pastebin.com/3YmycrWC

  • Why are you using callback if function getDictionary is asynchronous? has tried using the .then ?

  • I didn’t try, I’d be like?

  • I believe you’re wearing async and await wrong way, for example: await words.forEach(async palavra Particularly, I have always seen the use of both words in http/database requests, never with something different. Try to remove words from your code async and await places where you don’t need to use them. I hope this is it.

  • In fact, it’s not right to use async before a variable (example: async palavra). If you would like to see more content about: https://answall.com/questions/211505/como-posso-usuo-async-await-do-javascript

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.