Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
I can't understand why there is this output with 'foreach' in node.js

I'm new to node.js and I'm struggling with this problem. I don't understand why my output is

ciao
data
data

and not

data
data
ciao

This is my code

fs.readdir("sender", (err, files) => {
        if (err) {
            throw err;
        }
        const path = __dirname + "/sender/";

        let rawdata = fs.readFileSync(path + "data.json");
        var data = JSON.parse(rawdata);
        files.forEach((file) => {
            fs.stat(path + file, (err, stats) => {
                if (err) {
                    throw err;
                }
                if (data[file]["size"] != stats.size) data[file]["size"] = stats.size;
                if (data[file]["time"] != stats.mtime.toISOString()) data[file]["time"] = stats.mtime.toISOString();
                console.log("data");
            });
        });
        console.log("ciao");
    });

I've read that foreach is not asynchronous, so I don't really understand why the output is reversed.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

forEach is not asynchronous. But the operation you're doing in the forEach callback, fs.stat, is asynchronous.

So what your code is doing is starting a series of fs.stat operations (two, it looks like), then logging ciao, then later when each of those fs.stat operations completes, it's logging data.

You could use the promise-based file system API instead, and use async/await; see comments:

const {readdir, readFile, stat} from "fs/promises";

// This version of `readdir` returns a promise
fs.readdir("sender")
.then(async files => { // Use an `async` function as the fulfillment callback
    const path = __dirname + "/sender/";
    // Await the promise from `readFile` (no need to use `readFileSync`)
    const rawdata = await readFile(path + "data.json");
    const data = JSON.parse(rawdata);
    // Wait for all operations to complete (they run in parallel)
    await Promise.all(files.map(async file => {
        // Get the stats (again, awaiting the promise)
        const stats = await stat(path + file);
        // Update the entry for the file.
        const entry = data[file];
        if (entry.size != stats.size) { // (There's no point to this comparison, just do the assignment)
            entry.size = stats.size;
        }
        if (entry.time != stats.mtime.toISOString()) { // (Again)
            entry.time = stats.mtime.toISOString();
        }
        console.log("data");
    }));
    // Since this is after the `await` on the promise from `Promise.all`, it doesn't run until all
    // of the promises `Promise.all` was waiting on have been fulfilled
    console.log("ciao");
})
.catch(error => {
    // ...log/handle error...
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda