Use Promise.allSettled instead of Promise.all

This commit is contained in:
まっちゃとーにゅ 2023-11-21 21:09:29 +09:00
parent 1fa04c17d3
commit 4365d8d670
No known key found for this signature in database
GPG Key ID: 143DE582A97FE052
1 changed files with 10 additions and 6 deletions

View File

@ -177,7 +177,7 @@ export class NotificationEntityService implements OnModuleInit {
) : undefined; ) : undefined;
if (notification.type === 'reaction:grouped') { if (notification.type === 'reaction:grouped') {
const reactions = await Promise.all(notification.reactions.map(async reaction => { const reactions = await Promise.allSettled(notification.reactions.map(async reaction => {
const user = hint?.packedUsers != null const user = hint?.packedUsers != null
? hint.packedUsers.get(reaction.userId)! ? hint.packedUsers.get(reaction.userId)!
: await this.userEntityService.pack(reaction.userId, { id: meId }, { : await this.userEntityService.pack(reaction.userId, { id: meId }, {
@ -193,10 +193,11 @@ export class NotificationEntityService implements OnModuleInit {
createdAt: new Date(notification.createdAt).toISOString(), createdAt: new Date(notification.createdAt).toISOString(),
type: notification.type, type: notification.type,
note: noteIfNeed, note: noteIfNeed,
reactions, reactions: reactions.filter(result => result.status === 'fulfilled')
.map(result => (result as PromiseFulfilledResult<{ user: Packed<'User'>; reaction: string; }>).value),
}); });
} else if (notification.type === 'renote:grouped') { } else if (notification.type === 'renote:grouped') {
const users = await Promise.all(notification.userIds.map(userId => { const users = await Promise.allSettled(notification.userIds.map(userId => {
const packedUser = hint?.packedUsers != null ? hint.packedUsers.get(userId) : null; const packedUser = hint?.packedUsers != null ? hint.packedUsers.get(userId) : null;
if (packedUser) { if (packedUser) {
return packedUser; return packedUser;
@ -211,7 +212,8 @@ export class NotificationEntityService implements OnModuleInit {
createdAt: new Date(notification.createdAt).toISOString(), createdAt: new Date(notification.createdAt).toISOString(),
type: notification.type, type: notification.type,
note: noteIfNeed, note: noteIfNeed,
users, users: users.filter(result => result.status === 'fulfilled')
.map(result => (result as PromiseFulfilledResult<Packed<'User'>>).value),
}); });
} }
@ -280,9 +282,11 @@ export class NotificationEntityService implements OnModuleInit {
validNotifications = validNotifications.filter(x => (x.type !== 'receiveFollowRequest') || reqs.some(r => r.followerId === x.notifierId)); validNotifications = validNotifications.filter(x => (x.type !== 'receiveFollowRequest') || reqs.some(r => r.followerId === x.notifierId));
} }
return await Promise.all(validNotifications.map(x => this.packGrouped(x, meId, {}, { return (await Promise.allSettled(validNotifications.map(x => this.packGrouped(x, meId, {}, {
packedNotes, packedNotes,
packedUsers, packedUsers,
}))); }))))
.filter(result => result.status === 'fulfilled')
.map(result => (result as PromiseFulfilledResult<Packed<'Notification'>>).value);
} }
} }