I have the following code which makes 10 requests to randomuser API
using got
and async.series
. For some reasons, this only gives me one output. How do I go about to fix this?
const got = require('got');
const async = require('async');
var tenOperations = [];
for(var i = 0; i < 10; i++) {
tenOperations.push(doRequest);
}
function doRequest(callback) {
got('https://randomuser.me/api/')
.then(response => {
console.log(response.body);
})
.catch(error => {
console.log(error.response.body);
});
};
async.series(tenOperations, function(err, results) {
if(err) console.log(err);
console.log(results);
});
Here is the sample output
:
{
"results": [
{
"user": {
"gender": "female",
"name": {
"title": "miss",
"first": "غزل",
"last": "كامياران"
},
"location": {
"street": "6186 آزادی",
"city": "رشت",
"state": "تهران",
"zip": 64318
},
"email": "غزل.كامياران@example.com",
"username": "goldenpanda201",
"password": "muscles",
"salt": "OStU2tyA",
"md5": "92ac8a84380a24785597d0e916b0174e",
"sha1": "93f6e830538dbc557017011583cca3b5e527f854",
"sha256": "99a4c35237b1ebe276732fbf62efca24fd457428853de8a967dd465b80b82f0f",
"registered": 1352433856,
"dob": 1370066399,
"phone": "053-14062122",
"cell": "0929-641-1309",
"picture": {
"large": "https://randomuser.me/api/portraits/women/48.jpg",
"medium": "https://randomuser.me/api/portraits/med/women/48.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/48.jpg"
}
}
}
]
}