Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
Async/await logic in Javascript

I have a code that helps me rename multiple files with a data. In the data (rarityList) there is a name and a value for each file I have. For the sake of the post I just replaced the values with a dummy but you can imagine there is 500 of them.

It works and does the job but in a way that I did not intend. In the terminal it first prints 0(total) then copies all files and then renames it. But I was expecting to see something like Copying is done! and Renaming is done! and finally total number at the end of the terminal. Could you explain to me what I am missing here?

const fs = require("fs");
const path = "C:\\Users\\shepard\\Desktop\\Head";
const newPath = "C:\\Users\\shepard\\Desktop\\NewHead";
let files = fs.readdirSync(path);
const rarityList = [{name: "a", value: 3},{name: "b", value: 2}];
let total = 0;

const copy = async (_e) => {
    fs.copyFile(path + "\\" + _e, newPath + "\\" + _e, err => {if (err) {console.log(err)}});
  };

const rename = async (_e, _index) => {
    let t = _e.replace(".png", "#" + rarityList[_index].value + ".png")
    fs.rename(newPath + "\\" + _e, newPath + "\\" + t, err => {if (err) {console.log(err)}});
  };

files.forEach(async (e) => {
    await copy(e);
    console.log("Copying is done!");

    rarityList.forEach(async (element, index) => {
      if (element.name == e.slice(0,-4)) {
          await rename(e,index);
          console.log("Renaming is done! for " + e + " with the rarity weight of " + rarityList[index].value);
          total += rarityList[index].value;
      };
    });
});

console.log(total);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Possibly not the best answer, but a solution:

const fs = require("fs");
const path = "C:\\Users\\shepard\\Desktop\\Head";
const newPath = "C:\\Users\\shepard\\Desktop\\NewHead";
let files = fs.readdirSync(path);
const rarityList = [
  { name: "a", value: 3 },
  { name: "b", value: 2 },
];
let total = 0;

const copy = async (_e) => {
  fs.copyFile(path + "\\" + _e, newPath + "\\" + _e, (err) => {
    if (err) {
      console.log(err);
    }
  });
};

const rename = async (_e, _index) => {
  let t = _e.replace(".png", "#" + rarityList[_index].value + ".png");
  fs.rename(newPath + "\\" + _e, newPath + "\\" + t, (err) => {
    if (err) {
      console.log(err);
    }
  });
};

const renameFiles = async () => {
  for (let e of files) {
    await copy(e);
    let index = 0;
    for (let element of rarityList) {
      if (element.name == e.slice(0, -4)) {
        await rename(e, index);
        console.log(
          "Renaming is done! for " +
            e +
            " with the rarity weight of " +
            rarityList[index].value
        );
        total += rarityList[index].value;
      }
      index += 1;
    }
  }
  console.log('in async func', total);
};

renameFiles();
console.log('out of async function', total);

The trick is that the async function is not waited for, so total isn't updated at the "top level". You can do a "top level" await like this:

const fs = require("fs");
const path = "C:\\Users\\shepard\\Desktop\\Head";
const newPath = "C:\\Users\\shepard\\Desktop\\NewHead";
let files = fs.readdirSync(path);
const rarityList = [
  { name: "a", value: 3 },
  { name: "b", value: 2 },
];
let total = 0;

const copy = async (_e) => {
  fs.copyFile(path + "\\" + _e, newPath + "\\" + _e, (err) => {
    if (err) {
      console.log(err);
    }
  });
};

const rename = async (_e, _index) => {
  let t = _e.replace(".png", "#" + rarityList[_index].value + ".png");
  fs.rename(newPath + "\\" + _e, newPath + "\\" + t, (err) => {
    if (err) {
      console.log(err);
    }
  });
};

const renameFiles = async () => {
  for (let e of files) {
    await copy(e);
    let index = 0;
    for (let element of rarityList) {
      if (element.name == e.slice(0, -4)) {
        await rename(e, index);
        console.log(
          "Renaming is done! for " +
            e +
            " with the rarity weight of " +
            rarityList[index].value
        );
        total += rarityList[index].value;
      }
      index += 1;
    }
  }
  console.log("in function", total);
};

(async function() {
  await renameFiles()
  console.log(total)
}());
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!