refactor (backend): use Promise.all to await multiple async processes
This commit is contained in:
parent
26e35d1d82
commit
6e9d3387e0
1 changed files with 12 additions and 9 deletions
|
@ -22,8 +22,8 @@ async function recalculateNotesCountOfLocalUser(user: {
|
||||||
id: User["id"];
|
id: User["id"];
|
||||||
host: User["host"];
|
host: User["host"];
|
||||||
}) {
|
}) {
|
||||||
if (user.host == null) {
|
if (Users.isLocalUser(user)) {
|
||||||
await Notes.countBy({ userId: user.id }).then((newCount) =>
|
await Notes.countBy({ userId: user.id }).then((newCount: number) =>
|
||||||
Users.update(user.id, { updatedAt: new Date(), notesCount: newCount }),
|
Users.update(user.id, { updatedAt: new Date(), notesCount: newCount }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ export default async function (
|
||||||
async function findCascadingNotes(note: Note) {
|
async function findCascadingNotes(note: Note) {
|
||||||
const cascadingNotes: Note[] = [];
|
const cascadingNotes: Note[] = [];
|
||||||
|
|
||||||
const recursive = async (noteId: string) => {
|
const findRepliesAndQuotes = async (noteId: string) => {
|
||||||
const query = Notes.createQueryBuilder("note")
|
const query = Notes.createQueryBuilder("note")
|
||||||
.where("note.replyId = :noteId", { noteId })
|
.where("note.replyId = :noteId", { noteId })
|
||||||
.orWhere(
|
.orWhere(
|
||||||
|
@ -179,13 +179,16 @@ async function findCascadingNotes(note: Note) {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.leftJoinAndSelect("note.user", "user");
|
.leftJoinAndSelect("note.user", "user");
|
||||||
const replies = await query.getMany();
|
const repliesAndQuotes = await query.getMany();
|
||||||
for (const reply of replies) {
|
|
||||||
cascadingNotes.push(reply);
|
await Promise.all(
|
||||||
await recursive(reply.id);
|
repliesAndQuotes.map((n: Note) => {
|
||||||
}
|
cascadingNotes.push(n);
|
||||||
|
return findRepliesAndQuotes(n.id);
|
||||||
|
}),
|
||||||
|
);
|
||||||
};
|
};
|
||||||
await recursive(note.id);
|
await findRepliesAndQuotes(note.id);
|
||||||
|
|
||||||
return cascadingNotes;
|
return cascadingNotes;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue