0
promise in javaScript
im using fetch to get data from API, my code looks like, fetchResponse: function () { let url = "https://afzal-test-shop.myshopify.com/products/color_box.json"; let getResponse = fetch(url); getResponse.then((response) => { response.json(); }) .then((dataObj) => { return dataObj; // console.log(dataObj.product); }).catch(err=> console.log(err) ) return getResponse; }, i want to get result of data out of the function can anyone help me out.
3 Answers
0
In "getResponse", you are not returning the JSON object. You are just running the parser. Ditch the brackets.
0
Also, you don't need the second then.
0
And put "return" before "getResponse.then...". Remove the other return. That's your promise. When the promise is resolved. The "then" callback will invoke returning the object. Until then, you need the promise.