简体   繁体   中英

why it shows promise pending with Nodejs

Why the output shows "Promise { }" when it expected to shows "Bienvenue"

const translate = require('google-translate-api');

async function translateSentence(sentence, languebase, languagetranslation) {
    var sentenceTranslated = await translate(
        sentence,
        { from: languebase, to: languagetranslation });

    return sentenceTranslated.text;
}

var Translatedtext = translateSentence("Welcome", "en", "fr");
console.log(Translatedtext);

UPDATE:

I'm trying to return the translated data into JSON here is what I'm trying to do:

 data = new Object();
    data.Translatedtext = Translatedtext;

  var string = JSON.stringify(data);
  console.log(JSON.parse(string)); 

The out shows as { Translatedtext: {} } and I expected something like { Translatedtext: {Bienvenue} }

If your code is written with async/await it does not mean that you can get the value in not an async function. Your function just returns the Promise. Anyway you need to write like

translateSentence("Welcome", "en", "fr").then(text => console.log(text));

or call this function in the another async function

async anotherFunction() {
   const text = await translateSentence("Welcome", "en", "fr");
   console.log(text);
}

Because async functions return promises, so Translatedtext is a reference to a promise, and when you dump it out, you see the details of that promise, including the fact that it's pending.

If you meant to get the result , you'd have to await it (or use then ):

var Translatedtext = await translateSentence("Welcome", "en", "fr");
// ------------------^
console.log(Translatedtext);

(handling the exception if it rejects), or

translateSentence("Welcome", "en", "fr")
    .then(Translatedtext => {
        console.log(Translatedtext);
    })
    .catch(err => {
        // Handle the fact an error occurred
    });

Note that to use await , you'd have to be in an async function.

translateSentence is defined as async , thus it returns a Promise .

This means you need to wait for the promise resolve/reject before being able to access the result:

translateSentence("Welcome", "en", "fr")
  .then(Translatedtext => {
    var data = { Translatedtext };
    var string = JSON.stringify(data);
    console.log(JSON.parse(string)); 
  })
  .catch(err => console.log(err);

Assuming translate is returning a promise, we could make an async function and wait for translateSentence to do it's thing then add the text to the object.

This would be done like this:

async function performOperations(){
    let Translatedtext = await translateSentence("Welcome", "en", "fr")
    data = new Object();
    data.Translatedtext = Translatedtext;

    var string = JSON.stringify(data);
    console.log(JSON.parse(string));
}

performOperations()

What you are doing is checking the object before it has finished processing the request to the google api. So your data is more than likely getting to its location, its just that you're looking at the object too soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM