Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda