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

275
Visualizações
how to save local file content using fetch in javascript

I have a text.txt file and I want to save its content in a variable. My problem is that I can log the text but I can't store it in a var

let fileText;
fetch("./text.txt")
    .then(response => response.text())
    .then(text => fileText = text);
    console.log(fileText);  // undefined

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The console.log will be executed before the fetch promise resolved.

If you want line-by-line syntax execution you can use async/await.

let fileText;

(async () => {
const response = await fetch("./text.txt")
const text = await response.text()
fileText = text;
console.log(fileText);
})()
about 4 years ago · Santiago Gelvez 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