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

104
Visualizações
How to assign the output of a Fetch call to a variable (JavaScript)

I try to connect the output of a Fetch call to my page variable, but I just cannot seem to do it. Could you please check out the code underneath and help me out?

enter image description here

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

0

I think the async/await approach will solve your issues.

async outputvariable() {
  try {

     const response = await fetch('https://api2.online-conve', {
         method: 'GET',
         credentials: 'same-origin'
     });
      const rlt = await response.json();
      return rlt;
  } catch (error) {
   console.error(error);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You will have to make the surrounding function an async function, and then await the response.

async function(){
    try{
        var outputVariable = await fetch("URL").text();
        // Use outputVariable
    } catch(e){
        console.log('error', e);
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Or you can use the axios instead of the fetch.

import axios from 'axios';

const response = await axios('https://api2.online');

console.log(response.data);
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