If you want the result as soon as the first promise resolves with true
, you can do that too:
const somePromise = promises =>
new Promise((resolve, reject) => {
let resolveCount = 0;
const resolved = value => {
if (value) {
resolve(true);
} else if (++resolveCount === promises.length) {
resolve(false);
}
};
for (const promise of promises) {
promise.then(resolved, reject);
}
});
Alternative fancy approach:
const never = new Promise(() => {});
const somePromise = promises => Promise.race([
Promise.race(promises.map(async p => !!await p || never)),
Promise.all(promises).then(r => r.some(Boolean)),
]);
In your specific case, though, since there’s at most one promise, there’s a much better way to do it:
let hasPermission =
command.permissions.some(permissionsKey => {
switch (permissionsKey) {
case "all":
return true;
case "OWNER":
return false;
default:
return config.users_groups[permissionsKey].includes(msg.getStaticUserUID());
}
});
if (!hasPermission && command.permissions.includes("OWNER")) {
hasPermission = await msg.roomContext.isRoomOwnerId(msg.getStaticUserUID());
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…