diff --git a/apps/antalmanac/src/lib/helpers.ts b/apps/antalmanac/src/lib/helpers.ts index 25ea77d23..34d2af0ec 100644 --- a/apps/antalmanac/src/lib/helpers.ts +++ b/apps/antalmanac/src/lib/helpers.ts @@ -229,7 +229,7 @@ export interface Grades { gradeNPCount: number; } -const gradesCache: { [key: string]: Grades } = {}; +const gradesCache: { [key: string]: Grades | null } = {}; /* * Query the PeterPortal GraphQL API for a course's grades with caching @@ -240,14 +240,18 @@ const gradesCache: { [key: string]: Grades } = {}; * * @returns Grades */ -export async function queryGrades(deptCode: string, courseNumber: string, instructor = '') { +export async function queryGrades(deptCode: string, courseNumber: string, instructor = ''): Promise { instructor = instructor.replace('STAFF', '').trim(); // Ignore STAFF const instructorFilter = instructor ? `instructor: "${instructor}"` : ''; const cacheKey = deptCode + courseNumber + instructor; if (gradesCache[cacheKey]) { - return gradesCache[cacheKey]; + // If cache is undefined, there's a request in progress + while (gradesCache[cacheKey] !== null) { + await new Promise((resolve) => setTimeout(resolve, 200)); // Wait before checking cache again + } + return gradesCache[cacheKey] as Grades; } const queryString = `{