I have a component that allows users to drag and drop files or folders onto it, and my code then processes this information.
Currently, I am working on a recursive method that traverses through folders to collect all the files dropped by the user. The method looks like this:
private traverseDirectory(entry: FileSystemDirectoryEntry): Promise<FileSystemEntry[]> {
const reader = entry.createReader();
return new Promise<FileSystemEntry[]>(resolveDirectory => {
const iterationAttempts: Promise<FileSystemEntry>[] = [];
const errorHandler = () => {
logger.error('An error occurred while traversing the directory.');
};
const readEntries = () => {
reader.readEntries((batchEntries: FileSystemEntry[]) => {
if (!batchEntries.length) {
resolveDirectory(Promise.all(iterationAttempts));
} else {
batchEntries.forEach((batchEntry: FileSystemEntry) => {
if (batchEntry.isDirectory) {
iterationAttempts.push(
this.traverseDirectory(batchEntry as FileSystemDirectoryEntry)
);
} else {
iterationAttempts.push(Promise.resolve(batchEntry));
}
});
readEntries();
}
}, errorHandler);
};
readEntries();
});
}
I am looking for suggestions on how to improve the readability and cleanliness of this code, especially in removing the "as any" part from after the isDirectory() condition. Any help would be greatly appreciated!
As I am relatively new to Angular, any advice on enhancing my coding skills in general would also be very welcome. Thank you! :)