【发布时间】:2021-10-12 15:58:45
【问题描述】:
我的代码:
const attList = async (areaCode, cityCode) => {
const url = `http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList?ServiceKey=${serviceKey}&contentTypeId=12&areaCode=${areaCode}&numOfRows=40&sigunguCode=${cityCode}&MobileOS=ETC&MobileApp=AppTest`;
try {
const {data:res} = await axios.get(url)
const list = res.response.body.items.item;
return list
} catch (err) {
console.log(err);
}
}
console.log(attList(1, 1))
PromiseResult 没问题,但不知道如何从 Promise 中释放 PromiseResult{}
【问题讨论】:
-
console.log(attList(1, 1))----->attList(1, 1).then(data => console.log(data))。您还应该抛出来自attList函数的catch块的错误:console.log(err);---->throw error;。或者,您可以删除try-catch块并在调用此函数的代码中添加 catch 方法:attList(1, 1).then(data => console.log(data)).catch(...) -
您的
attList函数返回一个Promise,为了使用它,您需要在异步代码中使用await,或者在同步代码中使用.then()链接。 -
attList是异步函数,所以你可以等到 promise 完成后使用const result = await attList(1, 1)或await attList(1,1).then(result => {...})得到它的结果
标签: javascript reactjs async-await openapi