I'm trying to create an array of promises and call them with Promise.all.
I'm having trouble with correctly pushing the functions into the array, it seems they are being called instead of inserted and wait for Promise.all().
function findSpecialAbility(players, gameId, message) {
return new Promise(function(resolve, reject) {
let playersWithSpecials = _.reject(players, function(p) {
return p.role === 'alphaWolf' ||
p.role === 'betaWolf' ||
p.role === 'villager' ||
p.role === 'alchemist' ||
p.targetId === 0 ||
p.abilityUsed === true;
});
if (playersWithSpecials.length === 0) {
resolve();
} else {
let specialsToUse = [];
for (let i = 0, j = playersWithSpecials.length; i < j; i++) {
specialsToUse.push(useSpecialAbility(playersWithSpecials[i], gameId, message, players));
}
//Promise.all(specialsToUse).then(r = > console.log(r));
}
});
}
// Using promise below because some of the role will have database updates.
function useSpecialAbility(playerData, gameId, message, players) {
return new Promise(function(resolve, reject) {
if (playerData.role === 'seer') {
let getTargetData = _.find(players, {
id: playerData.targetId
});
message.guild.members.get(playerData.id).send(`Your target is a ${getTargetData.role}!`);
resolve('foo');
}
});
}