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

119
Visualizações
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 à 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