I have the following snippet of code to make multiple api calls
var userdetails:any=[]
for(var i=0;i<userids.length;i++)
{
userdetails.push(await this.getUserData(authToken,userid[i]))
}
Implementation of get userdetails function is as follows
async getUserData(authtoken,userid) {
return new Promise((resolve, reject) => {
const url = `https://***********/***/users?id=userid`;
const requestOptions = {
url,
method: 'GET',
headers: {
'Authorization': authtoken,
}
};
request(requestOptions, (err, response, body) => {
let errorMessage = 'Error in getting data';
if (err) {
return reject(err);
}
if (!response) {
return reject({
message: errorMessage
});
}
if (response.statusCode === 200) {
try {
body = JSON.parse(body);
} catch (err) {
reject({ message: errorMessage });
}
if (isArray(body)) {
let newArray: any = [];
body.forEach(element => {
newArray.push({
userId:element["userId"],
username:element["username"],
});
});
return resolve(newArray);
} else {
return resolve(body);
}
}
if (response.statusCode >= 400) {
return reject({
message: errorMessage
});
}
});
});
}
The above code works just fine and returns all the data. But there is a performance glitch in it and it is taking lot of time to return the data as the number of userid’s increase. I am already using async await approach. What can I do to tune the performance of this code?