问:
const getData = async () =>{
console.log('触发了getData接口')
let resultData = await getActivityInfo(activityId);
console.log(resultData,'resultData')
let id = resultData.id;
let shareImg = resultData.shareImg
let shareSubtitle = resultData.shareSubtitle
let shareTitle = resultData.shareTitle
console.log(id,shareImg,shareSubtitle,shareTitle,'-')
return {id,shareImg,shareSubtitle,shareTitle}
}
const {id,shareImg,shareSubtitle,shareTitle} = getData() console.log(id,shareImg,shareSubtitle,shareTitle,'?')
这段代码哪里有问题?为什么外面的输出没结果?
回答:
需要加上await getData()才能获取到数据,因为需要使用await等待异步请求返回数据然后再赋值,要是不使用await,就没有等待异步请求返回数据,所以就是undefined.