【发布时间】:2020-12-08 01:23:56
【问题描述】:
我需要在 .map 之后用数据完成后返回 temp_data 但是,现在它总是返回未定义的值,因为我正在这样做:let temp_data: LooseObject = {}。但是没有这个,我就无法使用 LooseObject。有没有什么技巧可以帮助我解决这个问题?
async getData(){
let temp_data: LooseObject = {}
this.categorylist.map((category: string) => {
this.getByCategory(category).then((newsList) => {
temp_data[category] = []
newsList.forEach((article: Article) => {
temp_data[category].push(article)
})
console.log('Data: ',temp_data)
})
})
return await temp_data
}
更新: 这是我的 getByCategory 函数的示例:
async getByCategory(category: string) {
const news = await this.newsapi.v2.topHeadlines({
q: category,
sortBy: 'popularity'
})
return await news.articles.map(this._transformArticle)
}
【问题讨论】:
标签: reactjs typescript async-await