0

I have a function will send all the images that located inside the "samples" folder to extractImage function, then, the extractImage function will call 3rd party API to perform its operation. But when I try to console.log(arr), it seem like didn't even be called. I think I have did something wrong to handling the async, could someone help me to have a look. I have quick new to JavaScript.

let arr = [];

await fs.readdir("samples", async (err, files) => {
  console.log(files);
  files.map(async (val) => {
    console.log(val);
    let tt = await extractImage(val);
    return arr.push(tt);
  });
});

fs.writeFileSync("final.json", "s");

console.log(arr);
console.log("tt");
2
  • 1
    That first await is not inside an async function, so it won't give the desired outcome Commented Sep 2, 2021 at 1:35
  • 1
    You didn't use fs.readdir() correctly, and it should be const files = await readdir(path); Commented Sep 2, 2021 at 2:02

4 Answers 4

1

You didn't use fs.readdir() correctly, and it should be const files = await readdir(path). And your files.map(async (val) => {...}) will return a list of promises immediately, and console.log(arr) might be executed even before these promises resolve. The code might be like the following one:


let files = await fs.readdir("samples")
console.log(files);

let promises = files.map(async (val) => {
    let tt = await extractImage(val);
    return tt;
});

let arr = await Promise.all(promises)
console.log(arr);
console.log("tt");

fs.writeFileSync("final.json", "s");

Sign up to request clarification or add additional context in comments.

Comments

1

I might consider using promisify in this instance, and then returning an array of promises from the files, and getting your data when all of those have been resolved.

const util = require('util');
const fs = require('fs');

const readdirP = util.promisify(fs.readdir);

async function main() {
  try {
    const files = await readdirP('samples');
    const promises = files.map(file => extractImage(file));
    const data = await Promise.all(promises);
  } catch(error) {
    console.log(error);
  }
}

main();

Comments

1

EDIT:

  1. await cannot be used in a global context.
  2. fs.readdir does not return a promise. It is callback based, which means you cannot await it. However, there is a synchronous version of the method: fs.readdirSync.
  3. As of Node 11, you can do this:
const fs = require('fs').promises

which allows the use of await and .then on fs. For lower versions, you can use util.promisify.

1 Comment

As I checked, the fs.readdir() does not support .then()
0

Using a self-invoked function could help in this case:

(async function () {
  let arr = [];
  await fs.readdir("samples", async (err, files) => {
    console.log(files);
    files.map(async (val) => {
      console.log(val);
      let tt = await extractImage(val);
      return arr.push(tt);
    });
  });
  fs.writeFileSync("final.json", "s");
  console.log(arr);
  console.log("tt");
});

1 Comment

I have tested this solution before but it seem like the console.log(arr); didn't wait for fs.readdir() to complete

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.