message: refactor: review comments of pr
diff: @@ -12,6 +12,7 @@ async function handleFileUpload(ctx) {
  const file = ctx.request.files;
  const fileName = Object.keys(file)[0];
  const filePath = file[fileName].path;
  + try {
  await delPrevFilesIfAny(fileName);
  ctx.response.status = 200;
  ctx.body = {
  @@ -20,18 +21,23 @@ async function handleFileUpload(ctx) {
  fileName,
  },
  };
  + } catch (error) {
  + ctx.response.status = 500;
  + }
  }
  -function delPrevFilesIfAny(currentFileName) {
  +async function delPrevFilesIfAny(currentFileName) {
  const uploadsDir = config.uploadsPath;
  - return getFileNames(uploadsDir).then(files => {
  + const files = await getFileNames(uploadsDir);
  const resourceId = currentFileName.split('-')[0];
  const filesToDel = files.filter(
  file => file.substr(0, file.indexOf('-')) === resourceId && file !== currentFileName
  );
  - filesToDel.map(async fileToDel => {
  + const deleteFile = promisify(fs.unlink);
  + return Promise.all(
  + filesToDel.map(fileToDel => {
  const filePathToDel = path.join(uploadsDir, fileToDel);
  - await promisify(fs.unlink)(filePathToDel);
  - });
  - });
  + return deleteFile(filePathToDel);
  + })
  + );
  }
  