I'm new to Promises and I'm having trouble resolving this problem. I have a basic function that essentially fetches some JSON using Axios:
function getData () {
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(function (response) {
var payload = response.data
return payload;
}).catch(function (response) {
return 'Promise Error'
})
}
If I invoke getData()
from another function, the payload keeps coming back undefined
.
(function getJSON () {
var returnedPayload = new Promise( function(resolve, reject) {
var payload = getData();
resolve(payload)
reject('Something went wrong')
})
returnedPayload.then(
console.log(payload)
).catch(
'Something went wrong'
)
console.log(returnedPayload)
})();
I know I'm doing something fundamentally wrong here, but I'm really not sure how to construct a promise within a promise...