I need to create file for each month, before next step of logic, but when I creating it inside for loop, it does not wait untill finish of this operation.
I tried to take for loop into separate async method, which I call inside main deleteAvatars method with "await".
private async s3ListsCreator(mainPathPart: string, type: string) {
const promises = Constants.MONTHS_FOLDERS.map(async folder => {
const s3Files: string[] = await this.getAllFiles(type, folder);
try {
await this.uploadS3FilesList(mainPathPart, s3Files, folder);
} catch (err) {
this.logger.error(err);
return this.response
.addError(new HttpErrors[400](`Upload on s3 list is unsuccessful`))
.build();
}
});
await Promise.all(promises);
}
async deleteAvatars(): Promise<Response> {
try {
await this.s3ListsCreator(fixPathPart, Constants.SUBFOLDERS.avatars);
} catch (err) {
this.logger.error(err);
return this.response
.addError(new HttpErrors[400](`Upload on s3 list is unsuccessful`))
.build();
}
try {
await this.filesCleanUpLambdaCall(
Constants.LIST_NAMES.avatarsList,
avatarResolutions,
Constants.SUBFOLDERS.avatars.concat(Constants.IMAGE, '/'),
);
} catch (err) {
this.logger.error(err);
return this.response
.addError(new HttpErrors[400](`Clean up is unsuccessful`))
.build();
}
}
I need to create all files before lambda call, but I create random number of files before I call lambda function.
logs:
- NB-010: https://s3.com/avatars/image/01/s3list.txt
- NB-010: https://s3.com/avatars/image/02/s3list.txt
- NB-010: https://s3.com/avatars/image/03/s3list.txt
- NB-010: https://s3.com/avatars/image/04/s3list.txt
- NB-010: https://s3.com/avatars/image/05/s3list.txt
- NB-010: https://s3.com/avatars/image/06/s3list.txt
- NB-010: https://s3.com/avatars/image/07/s3list.txt
- NB-010: https://s3.com/avatars/image/08/s3list.txt
- NB-010: https://s3.com/avatars/image/09/s3list.txt
- NB-010: https://s3.com/avatars/image/10/s3list.txt
- NB-010: lambda input:{"FunctionName":"filesCleanUp","Payload":"{\"listName\":\"avatars.txt\"}"}
- NB-010: https://s3.com/avatars/image/11/s3list.txt
- NB-010: https://s3.com/avatars/image/12/s3list.txt