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

104
Vistas
javascript using a fetch with a ternary operator

I need to get datas sometime from a file and othertime from an api with a fetch, I tried to use a ternary operator

import datafromFile from './Data/mydata.json';
let useDatafile = false; 
const dataToUse = useDataFile ? dataStored : fetchData().catch(console.error).then(result=>result);

but is going wrong on the following lines of code cause my dataToUse is still at promise my fetchData function looks like :

const fetchData = async () => {
            const response = await fetch(targetUrl, {
                method: "GET"
            });
            const myreturn = await response.json().then(result => {
                console.log('myreturn '); console.log(result);
                return result.data;
            });

        };

I used a ternary operator cause

 var dataToUse='';
 if (useDataFile) {
     dataToUse = dataStored;
   }
    else {
          //that's is probably horrible..
          fetchData().catch(console.error).then(result => dataToUse = result);
          }

gave me a dataToUse empty .. :-(

thank you for considering my code with indulgence I am really new to javascript, thank you for reading me and for your help

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