Transform the array into an array of Promises first, then call Promise.all on it, and check whether .some of the results array are truthy. You can avoid waiting for the whole array of Promises to resolve by checking whether .some of the items in the Promise array are truthy non-Promises, if you want:
cpermisPromises = command.permissions.map(permissionsKey => {
switch (permissionsKey) {
case "all":
{
return true;
}
case "OWNER":
{
return msg.roomContext.isRoomOwnerId(msg.getStaticUserUID());
}
default:
{
return config.users_groups[permissionsKey].includes(msg.getStaticUserUID());
}
}
});
if (cpermisPromises.some(result => result && typeof result.then !== 'function')) {
// at least one was truthy, don't need to wait for async call to complete
} else {
Promise.all(cpermisPromises).then((results) => {
if (results.some(result => result)) {
// at least one was truthy
}
});
}
Promise.all can accept arrays containing any values, but will wait for all Promise values to resolve before the Promise.all resolves.
Here's an alternative which allows you to check whether any of the synchronous values are truthy before running the Promises:
let found = false;
const promFns = [];
forloop:
for (let i = 0; i < command.permissions.length; i++) {
const permissionsKey = command.permissions[i];
switch (permissionsKey) {
case "all":
found = true;
break forloop;
case "OWNER":
proms.push(() => msg.roomContext.isRoomOwnerId(msg.getStaticUserUID()));
break;
default:
if (config.users_groups[permissionsKey].includes(msg.getStaticUserUID())) {
found = true;
break forloop;
}
}
}
if (found) {
// done, at least one truthy value was found synchronously
} else {
// need to run promises
Promise.all(
promFns.map(fn => fn())
)
.then((results) => {
if (results.some(result => result)) {
// done, at least one truthy value was found asynchronously
} else {
// no truthy value was found
}
});
}
Or, if want to send out requests serially, which might lead to fewer requests overall, but will take longer to complete, replace the Promise.all with:
let foundProm = false;
for (const fn of promFns) {
if (await fn()) {
foundProm = true;
break;
}
}
if (foundProm) {
// done, at least one truthy value was found synchronously
}else {
// no truthy value was found
}