What is the better way to handle the loop in node.js? For example, in the below code I am inserting the value into the new array. But the way I am returning the Promise seems to be an incorrect way to handle the loop, so is their any better option to do this?
let userProfileScore = [];
return new Promise(function (resolve, reject) {
for (let i = 0; i < userArr.length; i++) {
user.userProfile(userArr[i], (err, score) => {
if (err) throw err;
userProfileScore.push({
userID: userArr[i],
value: sco
});
if (i == userArr.length - 1) { // Here I am checking the key and then resolving the promise .
resolve(userProfileScore);
}
});
}
});