I am facing an issue while attempting to create an array of promises and then calling them using Promise.all
.
The problem lies in correctly pushing the functions into the array. It seems like they are getting executed instead of being inserted and waiting for Promise.all()
to handle them.
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');
}
});
}