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.

6th Aug 2022, 4:26 AM
Siddarth Mewara
3 Antworten
0
In "getResponse", you are not returning the JSON object. You are just running the parser. Ditch the brackets.
15th Aug 2022, 3:23 PM
Mustafa A
Mustafa A - avatar
0
Also, you don't need the second then.
15th Aug 2022, 3:23 PM
Mustafa A
Mustafa A - avatar
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.
15th Aug 2022, 3:27 PM
Mustafa A
Mustafa A - avatar