Node.js Promise.all Then not Firing

28 Views Asked by At

I am trying to do a very simple task using promises. I have multiple directories, each directory has a content.txt file in it. I need to find that file and return its URL. Once all URL's are returned I want to execute a function called 'task_ended'. My code works in that it returns all URL's, but I can't get my end function to fire.

This is my code

const fs = require("fs");
const glob = require("glob");

function getDirectoryName(dir) {
    return dir;
}

const startDir = getDirectoryName('compliance_finish');

const directoryFiles = fs.readdirSync(`../${startDir}`);

Promise.all(
directoryFiles.map((filename) => {
    return new Promise((resolve, reject) => {  
            let pat2 = ''
            getDirectories = function (src, callback) {
                glob(src + '/**/*', callback);
            };

            getDirectories(`../${startDir}/${filename}/`, function (err, res) {
                if(err){
                    console.log('Error', err);
                }else{
                    for(i=0;i<=res.length-1;i++){
                        if(res[i].includes('content.txt')){
                            pat2 = res[i]
                            console.log(pat2)
                        }
                    }
                }
            })     
    })
})
).then(() => {
    task_ended()
}, () => {
    
})

function task_ended(){
    console.log('finished')
}

1

There are 1 best solutions below

0
MickeyWoW On

Resolve function is missing.

getDirectories(`../${startDir}/${filename}/`, function (err, res) {
            if(err){
                console.log('Error', err);
                reject(err);
            }else{
                for(i=0;i<=res.length-1;i++){
                    if(res[i].includes('content.txt')){
                        pat2 = res[i]
                        console.log(pat2)
                    }
                }
                resolve("Success");
            }
        })