Answer the question
In order to leave comments, you need to log in
How to get array of values from async recursive function?
There is a function, recursive, that runs through the file system
const scanDir = async (dir, filelist = []) =>{
const files = await readdir(dir);
files.forEach(async (file)=>{
const dirFile = path.join(dir, file);
try {
filelist = await scanDir(dirFile, filelist);
}
catch (err) {
if (err.code === 'ENOTDIR' || err.code === 'EBUSY')
{
filelist = await [...filelist, dirFile];
}
else throw err;
}
});
return filelist;
};
const fs = require('fs')
,{promisify} = require('util')
,readdir = promisify(fs.readdir)
,exists = promisify(fs.exists)
,mkdir = promisify(fs.mkdir)
,copyFile = promisify(fs.copyFile);
const myArr = scanDir(dir);
(async()=>{
const myArr = await scanDir(dir);
})()
scanDir(dir).catch(
(err)=>{
console.log(err);
}
).then(
(data)=>{console.log(data.length)}
);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question