Skip to content

Instantly share code, notes, and snippets.

@quanfoo
Last active May 4, 2020 09:20
Show Gist options
  • Save quanfoo/e5a8029e88c72f1f70cdb9e14881640d to your computer and use it in GitHub Desktop.
Save quanfoo/e5a8029e88c72f1f70cdb9e14881640d to your computer and use it in GitHub Desktop.
const https = require('https');
const url = 'https://uinames.com/api/';
function request(url) {
return new Promise((resolve, reject) => {
let data = '';
https
.get(url, (res) => {
res.on('data', (chunk) => data += chunk);
res.on('end', () => resolve(JSON.parse(data)))
})
.on('error', (err) => reject(err));
});
}
const names = [];
const uniqueNames = 5;
for (let i = 0; i < uniqueNames; i++) {
request(url).then(success, error);
}
function success(d) {
if (names.length === uniqueNames) {
return;
}
if (!names.includes(d.name)) {
names.push(d.name);
} else {
request(url).then(success, error);
}
if (names.length === uniqueNames) {
console.log(names);
}
}
function error(e) {
console.log(e);
setTimeout(() => {
request(url).then(success, error);
}, 3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment