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'))