1

I want to call a function and return a value from another function the problem is that the value is empty

async function Print(){
  var result = await readFile();
  console.log(result);
}
async function readFile() {
  const FileName = ['g.png','g2.png'];
  const Files = FileName.length;
  texts = '';

  FileName.forEach( async function(FileName) {
     Tesseract.recognize(FileName,'ara',{ logger: m => console.log(m) }).then(({ data: { text } }) => {
     texts += text;
     console.log(texts);
})});
return texts;
}

1 Answers1

2

the following implementation using for of loop should work. Check out this post for more information.

async function readFile() {
  const FileName = ['g.png','g2.png'];
  const Files = FileName.length;
  let texts = '';
  
  for (fName of FileName){
    const { data: { text } } = await Tesseract.recognize(fName,'ara',{ logger: m => console.log(m)});
    texts += text;
    console.log(texts);
  }
  return texts;
}
Shakya Peiris
  • 504
  • 5
  • 11