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

182
Vistas
How to return a json from Fetch API call

My code looks like this

const TIME_REPEAT = 62e5

const getCities = () =>
        fetch('mocky url')
            .then(response => {
                console.log(response)
                return response.json()
            })
            .then(json => {
                console.log('hello json:', json)
                return json
            })

const cities = setInterval(getCities, TIME_REPEAT)

console.log('hello cities:', cities)

output

hello cities: Timeout {
  _idleTimeout: 6200000,
  _idlePrev: [TimersList],
  _idleNext: [TimersList],
  _idleStart: 4013,
  _onTimeout: [Function: getCities],
  _timerArgs: undefined,
  _repeat: 6200000,
  _destroyed: false,
  [Symbol(refed)]: true,
  [Symbol(asyncId)]: 14375,
  [Symbol(triggerId)]: 0
}

Last console statement should be showing the json which I want to pass down to my express router. But I don't get the desired output.

Reason why I used setInterval was because it does not return a promise. I want to get the cities json and pass it down to my express router.

What I did without setInterval

const cities = fetch('mocky url')
        .then(response => {
            console.log(response)
            return response.json()
        })
        .then(json => {
            console.log('hello json:', json)
            return json
        })

this cities object returned a promise. But I need a proper json. What can I use or do here to?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

SetInterval or say the polling approach is not a good idea to go with. That is sort of anti pattern when it comes to this sort of async handling.

You may go with async/await or promises as demonstrated below:

 const exec = async function(){
    const cities = await getCities();
    console.log(cities)
 }
 exec()

or

getCities.then((cities)=>{
  console.log(cities)
})
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