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

113
Visualizações
JavaScript rearange when file handeling

I'm trying to read an object from a JSON file and use it in the rest of the code. For that, I used "fs" with node.js. But it seems that javascript runs the selected part at the end.

const fs = require('fs');

let objList = [];

/* vvv Selected Part vvv */
fs.readFile("sample.json", (error, file) => {
    if (error) {
        console.log("ERROR!");
        throw error;
    }

    console.log("here");
    objList.push(JSON.parse(file.toString()));
    console.log("> ", objList);
});
/* ^^^^^^^^^^^ */

console.log(">> ", objList);

The ouput is:

>>  []
here
>  [ { name: 'name goes here', age: 30 } ]

Why does it happen and how can I fix that? Beside, Is there a better way to implement this?

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

0

There's nothing wrong with how your code works, it's not a bug

The function fs.readFile reads a file asynchronously, meaning it's not blocking so your program continues, and the function you pass as the second parameter is a callback that is fired once reading the file is finished.

There are a couple of ways you can deal with it: One of them is to use the fs.readFileSync function, which does the same thing but synchronously, meaning it stops your program until reading the file is done. This is usually not recommended since reading files may take time and it's better to do it in a non-blocking way. It would look something like

const fs = require('fs');

let objList = [];

const file = fs.readFileSync("sample.json",   {encoding:'utf8'})
objList.push(JSON.parse(file.toString()))
console.log(">> ", objList);

You can add a try/catch block to catch errors as well

The other method is to use an async function and the await keyword to wait for your file to be read use fs.promises like so:

const fs = require('fs');

async function myFunc() {
   let objList = [];
   const file = await fs.promises.readFile("sample.json","utf8")
   objList.push(JSON.parse(file.toString()))
   console.log(">> ", objList);
}

myFunc()

Which uses a Promise that you can wait until it's finished

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