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

117
Vistas
making an api call and returning a promise

I have a function which will take a city name as input and returns a promise that will resolve with the result of an api call that returns current weather for the city, and reject if the api call fails or the input string is invalid

When I call the function, I attach .then and .catch handlers to catch any errors and console log the response of the api call

Is there a way to do what the .then and .catch handlers do within the function body itself so i can just call the function without attaching those handlers?

const cityWeather = (city) => {
  return new Promise((resolve, reject) => {
    if (typeof city !== 'string') reject(Error('not a string'))
    axios.get(`http://api.weatherapi.com/v1/current.json?key=${api_key}&q=${city}&aqi=no`).then((response) => {
      const {data: {current}} = response
      resolve(current)
    }).catch((err) => reject(err))
  })
}

cityWeather(44).then((response) => console.log(response)).catch((reason) => console.log('hello'))

sandbox

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