Skip to content

Instantly share code, notes, and snippets.

@shsunmoonlee
Last active June 26, 2021 17:31
Show Gist options
  • Save shsunmoonlee/96deb3cfc5a6be3abfa224b64b00d687 to your computer and use it in GitHub Desktop.
Save shsunmoonlee/96deb3cfc5a6be3abfa224b64b00d687 to your computer and use it in GitHub Desktop.
How to asynchronously use nested http.get request using native node.js method.
/*
* Complete the function below.
* Instead of returning the answer, log the answer to the console.
* https://jsonmock.hackerrank.com/api/countries/search?name=
*/
function getCountries(s, p) {
let answer = 0;
const https = require("https");
let base_url = `https://jsonmock.hackerrank.com/api/countries/search`;
function getTotalPages(s) {
let url = `${base_url}?name=${s}`;
return new Promise((resolve, reject) => {
https.get(url, res => {
let response = "";
res.on("data", chunk => {
response += chunk;
});
res.on("end", () => {
resolve(JSON.parse(response).total_pages);
});
});
});
}
function getCountriesPerPage(s, page) {
let url = `${base_url}?name=${s}&page=${page}`;
return new Promise((resolve, reject) => {
https.get(url, resp => {
let data = "";
resp.on("data", chunk => (data += chunk));
resp.on("end", () => resolve(JSON.parse(data).data));
});
});
}
getTotalPages(s).then(
total_pages => {
let current_page = 1;
let countries = [];
while (current_page <= total_pages) {
countries.push(getCountriesPerPage(s, current_page));
current_page++;
}
Promise.all(countries).then(arr => {
let answer = arr
.reduce(
(accumulator, currentValue) => [...accumulator, ...currentValue],
[]
)
.filter(item => item.population > p).length;
console.log(answer);
return answer;
});
},
err => console.log(err)
);
}
console.log("===final answer", getCountries("in", 10000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment