Home > Enterprise >  Object Promise returned and output rather than value
Object Promise returned and output rather than value

Time:08-19

I have been looking through promise questions for the last hour and am totally clueless so I decided to write a question as I am unable to store the value of the function in a variable without it resulting in a promise.

const T = require("tesseract.js");

async function imageCheck(T, url){
    T.recognize(url, 'eng')
    .then(out => {return(out.data.text)});
}
url = imageCheck(T, 'EXAMPLEURL');
Promise.resolve(url)
console.log("the url is " url)

My output is:

the url is [object Promise]

CodePudding user response:

The Promise.resolve() will return another Promise. You should use then to get value.

MDN Documentation

I would recommend the following ways.

const T = require("tesseract.js");

async function imageCheck(T, url){
    const out = await T.recognize(url, 'eng');
    return out.data.text;
}

imageCheck(T, 'EXAMPLEURL')
  .then( url => console.log("the url is " url));

OR

const T = require("tesseract.js");

async function imageCheck(T, url){
    return T.recognize(url, 'eng');
}

imageCheck(T, 'EXAMPLEURL')
   .then(out => console.log("the url is " out.data.text));

  • Related