Code for the following question here: https://bit.ly/2ZC8MZ5
I am trying to write a script that does two separate API calls.
The first call gets data and then passes that to a second call to a different API.
I am trying to console.log
the results of the second API call but all I’m getting is => Promise{pending}
. In the browser the second call is resolved and the data can be seen in the console. However, in node the promise is pending.
Any ideas why? My code is below. Many thanks!
//dependencies
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const xhr = new XMLHttpRequest();
// variables
const astrosUrl = 'http://api.open-notify.org/astros.json';
const wikiUrl = 'https://en.wikipedia.org/api/rest_v1/page/summary/';
//getJSON
getJSON = (url) => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(url);
xhr.open('GET', url);
xhr.onload = () => {
if(xhr.status === 200){
let data = JSON.parse(xhr.responseText);
resolve(data);
}else{
reject(new Error(xhr.statusText));
}
}
xhr.send();
})
}
//get wiki data
getProfiles = (data) =>{
const profiles = data.people.map(person => {
return getJSON(wikiUrl+formatName(person.name));
})
return Promise.all(profiles);
}
//handle data
handleData = (data) =>{
console.log(data);
}
//HELPER FUNCTIONS
formatName = (name)=>{
return name.split(" ").join("_");
}
//run sequence
getJSON(astrosUrl)
.then(getProfiles)
.then(handleData)
.catch(error => console.log(error.message))