0
exports.getNews = async (category) =>{ //get a promise from api for display it in mustach template

  exports.fetch_news_from_API =  async (category) => { // get data from api
    /* get data from api*/
    return data.articles; // promise
  }

  exports.getData = async (data) =>{ // add an id for every news
    let finalData = [];
    for(let i = 0; i < data.length; i++){
      let news = {
        id : i,
        title : data[i].title,  //api object
        author : data[i].author,
        description : data[i].description,
        publishedAt : data[i].publishedAt,
      }
      finalData.push(news);
    }
    return finalData; // news
  }
  
  let data = await this.fetch_news_from_API(category);
  let news = await this.getData(data);
  return news; // return pending all the time whene i use it in app.get
}

I would like to use "news" to display it but I can not because it returns all the time "pending" thank you in advance

  • At the top level, a caller must say `fetch_news_from_API(category).then(news => /* use the news here */)`. In another async function, a caller can say `let news = await fetch_news_from_API(category)` – danh Apr 23 '22 at 13:51
  • Do not assign to `exports` from inside a function! – Bergi Apr 23 '22 at 13:52
  • @danh Why? Top-level `await` is supported by all maintained Node.js versions (available since Node.js v14.8). – jabaa Apr 23 '22 at 13:53
  • Why is `getData` async? Unrelated, but it’s usually best to pick a single naming convention instead of switching_back andForth. – Dave Newton Apr 23 '22 at 14:01
  • the problem is when i use it in `app.get('/', (req, res) => { res.render('index', {"data": model.getNews(category)}); })` it return { }, i saw that i can do this:`model.getNews(category).then((result){ console.log(result)})` but what about the return statement ? – killian rando Apr 23 '22 at 14:27

0 Answers0