0

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:

1
  • I'm sorry that I don't have time to give a proper answer right now, but you should look into Promise.all Commented Oct 15, 2019 at 11:09

1 Answer 1

1

if you convert the for loop into map then you can use Promise.all to solve that

const promises = array.map(delayedLog);
// wait until all promises are resolved
await Promise.all(promises);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.