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

120
Visualizações
Global variable undefined within function

I'm trying to store the lines of a local file as an array (where each line is a separate element). When I try to access the array later, I recieve an error, saying that the array is undefined.

var lines;

fetch("textfile.txt")
    .then((response) => {
        return response.text();
    })
    .then((text) => {
        lines=text.split('\n');
      console.log(text);  //Prints the text in the file
      console.log(lines); //Prints the correct array
    });
  
console.log(lines); //Print undefined

How can I fix this so I can use the lines variable elsewhere. Thanks in advance for any help.

about 4 years ago · Juan Pablo Isaza
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