1

I want it to log the ai generated text after the api has returned something but it doesn't seem to be waiting for the variable and only logs it after waiting.

//don't use await if you are using then operator on promise
function lyrics(message) {
    var resp = deepai.callStandardApi("text-generator", {
            text: fs.createReadStream("./Images/Elma.txt"),
    }).then(
        console.log(resp)
    ).catch(err => console.log(err));
    console.log(resp);
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Check how to use async-await and then with promises. Please view on https://stackoverflow.com/questions/55019621/using-async-await-and-then-together#:~:text=3%20Answers&text=An%20async%20function%20can%20contain,and%20returns%20the%20resolved%20value, this question might help you. – Apoorva Chikara Feb 28 '21 at 07:07

1 Answers1

1

As it's an async call which its result might returned only after the async call, don't use the returned value; You should specify & use the then's callback param as follows:

function lyrics(message) {
    deepai.callStandardApi("text-generator", {
        text: fs.createReadStream("./Images/Elma.txt"),
    }).then(resp)(
        console.log(resp)
    ).catch(err => console.log(err));
}

BTW, notice that you don't use the declared message param.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108