Skip to content

Commit

Permalink
feat: make main thread more asynchronous
Browse files Browse the repository at this point in the history
Avoid the use of await within the for loop to fire the API calls off
asynchronously
  • Loading branch information
trev-dev committed Jul 19, 2023
1 parent 5b66fe4 commit 524db4f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions tools/onboarding/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ async function onboardUser(record: UserRecord, instituteType: InstituteTypeCode)

async function main(instituteType: InstituteTypeCode) {
const userRecordStream = createCSVReadStream('./user-records.csv');
let totalUsers = 0;
let usersOnboarded = 0;
let queuedUsers: Promise<boolean>[] = [];

console.log(`::: Begin Onboarding for ${instituteType} Users:::`)
for await (const record of userRecordStream) {
totalUsers++;

if (isUserRecord(record)) {
const onboarded = await onboardUser(record, instituteType);
if (onboarded) usersOnboarded++;
queuedUsers.push(onboardUser(record, instituteType));
} else {
console.log(`Malformed data in CSV row: ${JSON.stringify(record)}`);
}
}

const completedQueue = await Promise.all(queuedUsers);
const totalUsers = completedQueue.length;
const usersOnboarded = completedQueue.filter(u => u === true).length;

console.log(`::: Users Onboarded: ${usersOnboarded}/${totalUsers}:::`);
}

Expand Down

0 comments on commit 524db4f

Please sign in to comment.