From d47854e620085bbdce58636ee29c53a8cfbb4a09 Mon Sep 17 00:00:00 2001 From: Menci Date: Fri, 26 Apr 2019 10:57:11 +0800 Subject: [PATCH] Await promises in parallel --- models/common.ts | 15 +++++++++------ models/judge_state.ts | 10 ++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/models/common.ts b/models/common.ts index 4317153..c50882b 100644 --- a/models/common.ts +++ b/models/common.ts @@ -173,12 +173,15 @@ export default class Model extends TypeORM.BaseEntity { result.meta.top = result.data[0].id; result.meta.bottom = result.data[result.data.length - 1].id; - result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :id`, { - id: result.meta.top - }).take(1).getOne()); - result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :id`, { - id: result.meta.bottom - }).take(1).getOne()); + // Run two queries in parallel. + await Promise.all(([ + async () => result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :id`, { + id: result.meta.top + }).take(1).getOne()), + async () => result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :id`, { + id: result.meta.bottom + }).take(1).getOne()) + ]).map(f => f())); return result; } diff --git a/models/judge_state.ts b/models/judge_state.ts index 7564443..ad7868a 100644 --- a/models/judge_state.ts +++ b/models/judge_state.ts @@ -133,13 +133,15 @@ export default class JudgeState extends Model { if (this.type === 0) { await this.loadRelationships(); - // No need to await them. - this.user.refreshSubmitInfo(); - this.problem.resetSubmissionCount(); + const promises = []; + promises.push(this.user.refreshSubmitInfo()); + promises.push(this.problem.resetSubmissionCount()); if (!newSubmission) { - this.problem.updateStatistics(this.user_id); + promises.push(this.problem.updateStatistics(this.user_id)); } + + await Promise.all(promises); } else if (this.type === 1) { let contest = await Contest.findById(this.type_info); await contest.newSubmission(this);