Review a generated profile loader

from The event loop
Node 24 advanced 8 min 5 issues to find

Review this generated Node.js loader against the task and identify five distinct risks.

Load user-selected JSON profiles concurrently, preserve input order, reject invalid or missing files, and allow no more than four reads at once.

JavaScript
const { readFile } = require('node:fs');

async function loadProfiles(ids) {
  const profiles = [];
  ids.forEach(async (id) => {
    const profile = await readProfile(id);
    profiles.push(profile);
  });
  setTimeout(() => console.log('loaded'), 100);
  return profiles;
}

function readProfile(id) {
  return new Promise((resolve) => {
    readFile(`profiles/${id}.json`, 'utf8', (error, text) => {
      if (error) {
        resolve(null);
        return;
      }
      resolve(JSON.parse(text));
    });
  });
}

loadProfiles(process.argv.slice(2)).then(console.log);

generated code is illustrative, not from any one model

Open in playground
Report an error