【发布时间】:2022-01-07 08:10:17
【问题描述】:
我是 Promise 的新手,想知道如何将响应的结果存储在外部变量中
var obj;
fetch("https://api.nvidia.partners/edge/product/search?page=1&limit=9&locale=fr-fr&category=GPU&gpu=RTX%203070&manufacturer=NVIDIA&manufacturer_filter=NVIDIA~1,ASUS~1,EVGA~3,GIGABYTE~2,MSI~1,PNY~0,ZOTAC~0")
.then(res => res.json())
.then(data => obj = data.searchedProducts.featuredProduct.retailers[0].purchaseLink)
.then(() => console.log(obj))
console.log(obj);
我设法在承诺中显示响应,但不在外部
the undefined value comes from the second console.log
感谢您花时间阅读我的请求 :)
【问题讨论】:
-
您的第二个“console.log(obj)”在设置 obj 的值之前执行,因此在记录时它仍然未设置
标签: javascript promise fetch-api