I have a search form that returns a result after going through 3 Async APIs (Spotify, Twitter and Google Sentiment Analysis).
The issue: The result is rendered on the page too late whereby I am getting undefined values (while in console log the values are returned correctly after a delay)
Question: How do I wait for the values to be returned before i use res.render() to display on page?
Code:
router.post('/', (req, res) => { 
    const sentiment = sentimentAnalyze(req.body);
     res.render('home', { name: sentiment.artistName, score:sentiment.score, mag:sentiment.mag});
});
async function sentimentAnalyze(searchValue){
    try {
        const artist = await searchedArtist(searchValue);
        const tweetsDoc= await getTwitterResults(artist);
        const sentiment = await googleAnalyze(tweetsDoc);
        return sentiment;
    } catch(err) {
        console.log('Error::: ', err);
    } 
}
function searchedArtist(searchValue) {
    return new Promise((resolve, reject) => {
        if(searchValue != null) {   
            let name = searchValue.name;
            let twitterResults;
            // Get artist
            getArtist(name, "artist").then((response) => {
                data = {
                "id": response.artists.items[0].id,
                "name":  response.artists.items[0].name
                }
                // resolve and return the aritst object
                resolve(data);
            });
        }
    });
}
function getTwitterResults(data) {
    return new Promise((resolve, reject) => {
        client.get('search/tweets', {q: data.name}, function(error, tweets, response) {
            let resultObject = search("", tweets.statuses);
            //  console.log(resultObject); 
            //  The text to analyze
            const document = {
                content: resultObject,
                type: 'PLAIN_TEXT',
                artistName: data.name
            };
            resolve(document);
        });
    });
}
// Detects the sentiment of the text
function googleAnalyze(document) {
    return new Promise((resolve, reject) => {
      // Detects the sentiment of the text
      googleClient
        .analyzeSentiment({document: document})
        .then(results => {
          const sentiment = results[0].documentSentiment;
          console.log(`Sentiment score of ${data.name}: ${sentiment.score}`);
          console.log(`Sentiment magnitude: ${sentiment.magnitude}`);     
          // return sentiment reponse 
          resolve(sentiment);
        })
        .catch(err => {
          console.error('GOOGLE API ERROR:', err);
        });
    });
 }
Please help, new to Async Js!
 
    