Reputation: 557
This is my axios function
const requestAllSubPosts = (sub,epoch,count) => {
const res = {};
const sub_url = `https://api.pushshift.io/reddit/search/submission/?subreddit=${sub}&limit=1000&sort=desc&before=${epoch}`;
return axios
.get(sub_url)
.then(response => {
const d = response.data.data;
if(d.length > 0) {
subFetchEpoch = d[d.length - 1].created_utc;
subDataArray = subDataArray.concat(d);
count++;
requestAllSubPosts(sub,subFetchEpoch,count)
}
else {
console.log('data-empty');
res.data = subDataArray;
res.epoch = subFetchEpoch;
console.log(res); <-- WORKING
return res ; <-- NOT WORKING
}
})
.catch(error => {
return error.message
});
}
I'm using axios in a sort of loop, and fetching data till the response is empty. After that I want to return the appeneded data in the parent function.
This is the parent function. I call on axios and logging the response. But I'm not getting any response
async function fetchAllSubPosts (sub, epoch) {
const today = moment(new Date()).valueOf();
// let ep_a = moment(ep).valueOf()
let dataArray = [];
let resp = await requestAllSubPosts(sub, ep, 1)
console.log({resp}); <-- NOT WORKING
}
Upvotes: 0
Views: 60
Reputation: 171669
When you recursively call requestAllSubPosts()
the then()
has no return
and so by default will return undefined
to the next then()
or await
in the chain
You want to return
the promise from the recursive call.
Change:
requestAllSubPosts(sub,subFetchEpoch,count)
To
return requestAllSubPosts(sub,subFetchEpoch,count)
Upvotes: 1