Last active
May 4, 2020 09:20
-
-
Save quanfoo/e5a8029e88c72f1f70cdb9e14881640d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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