I have a following code which creates remote folders locally if they do not exist:
await remoteFolders
.filter(rf => !localFolders.some(lf => lf === rf))
.forEach(async (rf) => await this.createFolder(rf));
Method createFolder looks like this:
async createFolder(folderPath) {
if (!(await fs.exists(folderPath))) {
await fs.mkdir(folderPath);
}
}
The problem is that if I debug I can see that await in forEach is ignored and the loop does not wait until createFolder method finishes. So if I create directories which depend on previous ones, it obviously fails.
But if I use for-loop it works fine (await is taken into account and loop execution continues after createFolder finishes):
for (let i = 0; i < remoteFolders.length; i++) {
if (!localFolders.some(lf => lf === remoteFolders[i])) {
await this.createFolder(remoteFolders[i]);
}
}
I can use this one, but using forEach looks more elegant to me. Why does not forEach take await into account?