I need an asynchronous "getTypes" function that analyzes the list of passed paths and returns an array describing what is in each of the paths. Function should work well in any case. If an error occurs during the asynchronous operation, the value for this path is "null". I tried to write a function, but it doesn't work.
const getTypes = (arr) => {
const prom = arr.reduce((acc, path) => (
acc.then((data) => {
return fs.stat(path).isFile() ? 'file' : 'directory';
})
.catch(() => null)
), Promise.resolve());
return prom;
}
How it should work:
getTypes(['/etc', '/etc/hosts', '/undefined']).then(console.log);
// ['directory', 'file', null]
Don't know how to do it, can anyone help? plz