Browse Source

Await promises in parallel

pull/6/head
Menci 6 years ago
parent
commit
d47854e620
  1. 11
      models/common.ts
  2. 10
      models/judge_state.ts

11
models/common.ts

@ -173,12 +173,15 @@ export default class Model extends TypeORM.BaseEntity {
result.meta.top = result.data[0].id; result.meta.top = result.data[0].id;
result.meta.bottom = result.data[result.data.length - 1].id; result.meta.bottom = result.data[result.data.length - 1].id;
result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :id`, { // Run two queries in parallel.
await Promise.all(([
async () => result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :id`, {
id: result.meta.top id: result.meta.top
}).take(1).getOne()); }).take(1).getOne()),
result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :id`, { async () => result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :id`, {
id: result.meta.bottom id: result.meta.bottom
}).take(1).getOne()); }).take(1).getOne())
]).map(f => f()));
return result; return result;
} }

10
models/judge_state.ts

@ -133,13 +133,15 @@ export default class JudgeState extends Model {
if (this.type === 0) { if (this.type === 0) {
await this.loadRelationships(); await this.loadRelationships();
// No need to await them. const promises = [];
this.user.refreshSubmitInfo(); promises.push(this.user.refreshSubmitInfo());
this.problem.resetSubmissionCount(); promises.push(this.problem.resetSubmissionCount());
if (!newSubmission) { 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) { } else if (this.type === 1) {
let contest = await Contest.findById(this.type_info); let contest = await Contest.findById(this.type_info);
await contest.newSubmission(this); await contest.newSubmission(this);

Loading…
Cancel
Save