Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Ensure that children functions have terminated

I have a function that walks recursively over a folder, performing an expensive operation on each file. So far, the function was simple and pretty:

import fs from 'fs';
async function openPath(path) {
    // Path is a directory
    if (fs.statSync(path).isDirectory()) {
        let subPaths = fs.readdirSync(path);
        for (const file of subPaths) await openPath(path + '/' + file);
    // Path is a file
    } else await expensiveOperationWithDB(path);
}

Realizing that I only needed file processing to be synchronized when the files were on the same directory, I came up with this ugly function:

async openPath(path, isFile) {
        if (isFile) await expensiveOperationWithDB(path);
        else {
            const subPaths = fs.readdirSync(path);
            for (let newPath of subPaths) {
                newPath = path + '/' + newPath;
                if (fs.statSync(path).isFile()) {
                    await this.openPath(newPath, true);
                } else {
                    this.openPath(newPath, false);
                }
            }
        }
    }

However, the function should return only when its children functions have terminated. How can this be implemented?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You'll want to collect all the promises from any called functions into an array and use Promise.all on them.

import fs from 'fs/promises';
async function openPath(path) {
    const stat = await fs.stat(path);
    if (stat.isDirectory()) {
        return openDirectory(path);
    } else if (stat.isFile()) {
        return expensiveOperationWithDB(path);
    }
}
async function processDirectoryFiles(filePaths) {
    for (const path of filePaths) {
        await expensiveOperationWithDB(path);
    }
}
async function openDirectory(path) {
    const names = await fs.readdir(path);
    const pathStats = await Promise.all(names.map(name => {
        const newPath = path + '/' + file;
        const stat = await fs.stat(newPath);
        return {
            path: newPath,
            isFile: stat.isFile(),
            isDirectory: stat.isDirectory();
        };
    }
    const promises = [];
    const filePaths = [];
    for (const {path, isDirectory, isFile} of pathStats) {
        if (isDirectory) {
            promises.push(openDirectory(path));
        } else if (isFile) {
            filePaths.push(path);
        }
    }
    promises.push(processDirectoryFiles(filePaths));
    return Promise.all(promises);
}
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!