Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!