Skip to content

Commit

Permalink
fix eslint issues. use promise.all instead of await in a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
JeelRajodiya committed Nov 22, 2024
1 parent 8cb082b commit b656d31
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions scripts/casestudies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import { readdir, readFile, writeFile } from 'fs/promises';

import { convertToJson } from '../utils';

export async function buildCaseStudiesList(dirWithCaseStudy, writeFilePath) {
export async function buildCaseStudiesList(dirWithCaseStudy: string, writeFilePath: string) {
try {
const files = await readdir(dirWithCaseStudy);
const caseStudiesList = [];

for (const file of files) {
const caseStudyFileName = [dirWithCaseStudy, file].join('/');
const caseStudyContent = await readFile(caseStudyFileName, 'utf-8');
const jsonContent = convertToJson(caseStudyContent);
// Process all files in parallel using Promise.all
const caseStudiesList = await Promise.all(
files.map(async (file) => {
const caseStudyFileName = [dirWithCaseStudy, file].join('/');
const caseStudyContent = await readFile(caseStudyFileName, 'utf-8');

caseStudiesList.push(jsonContent);
await writeFile(writeFilePath, JSON.stringify(caseStudiesList));
}
return convertToJson(caseStudyContent);
})
);

// Write the complete list once after all files are processed
await writeFile(writeFilePath, JSON.stringify(caseStudiesList));

return caseStudiesList;
} catch (err) {
throw new Error(err);
throw new Error(err instanceof Error ? err.message : String(err));
}
}

0 comments on commit b656d31

Please sign in to comment.