-1

How can I run Array.map off of an await?

const CLASS_PATH = 'User/matt/Github/project';
const PACKAGE_JSON = 'package.json';

const walk = async path => {
  let dirs = [];
  for (const file of await readdir(path)) {
    if ((await stat(join(path, file))).isDirectory()) {
      dirs = [
        ...dirs,
        file,
      ];
    }
  }
  return dirs;
};


async function main() {
  const packagePaths = await walk(CLASS_PATH)
        .map(pkgName => join(CLASS_PATH, pkgName, PACKAGE_JSON));

}
main();
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233

2 Answers2

5

Parenthesis () can always be used to change operator predescendence:

 (await walk(CLASS_PATH)).map(/*...*/)
jbyrd
  • 5,287
  • 7
  • 52
  • 86
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

You can also use Promise. So in your case:

const packagePaths = await Promise.all(walk(CLASS_PATH).map(async (item): Promise<number> => {
    await join(CLASS_PATH, pkgName, PACKAGE_JSON);
}));

The Promise.all(iterable) method returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.

Dorian Mazur
  • 514
  • 2
  • 12