I'm new to promises and wondering how to store the result of a response in an extern variable
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);
I manage to display the response in the promise but not outside
the undefined value comes from the second console.log
Thank you for taking the time to read my request :)
What you can do is await it:
const res = await 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");
const data = await res.json();
const obj = data.searchedProducts.featuredProduct.retailers[0].purchaseLink;
console.log(obj);
Then you can just console.log() it.