I am trying to execute the following code block:
this.setState({ // <------------ REF 1
pages: pages
});
only after all
axios.get('https://graph.facebook.com/v5.0/' + page.id + '/events?access_token=' + accessToken)
requests have finished. REF 2
How can I achieve this in Javascript?
let accessToken = response.accessToken
axios.get('https://graph.facebook.com/v5.0/me/accounts?fields=id,name&access_token=' + response.accessToken)
.then(response => {
console.log("test")
let pageList = response.data.data.map(page => {
console.log("page " + page.id + " " + page.name);
this.setState({
page: response.data.data
});
var pages = {}; // <------------ REF 2
axios.get('https://graph.facebook.com/v5.0/' + page.id + '/events?access_token=' + accessToken)
.then(response => {
console.log("test 2");
var events = {};
let eventList = response.data.data.map(event => {
console.log("event " + event.id + " " + event.name);
events[event.id] = event.name;
});
pages[page.id] = [page.name, events];
})
this.setState({ // <------------ REF 1
pages: pages
});
})
})