In a Vue.js component, I have some methods that use axios to call an API.
In different cases, I need to execute some code once the call in this method has resolved, but I don't want to add a bunch of if
statements in the .then()
chained to the axios call.
methods: {
callApi() {
axios.get('/api')
.then(() => {
// call has resolved, request is done
})
},
firstMethod() {
this.callApi()
// Need to wait for the call to resolve
// Do something
},
secondMethod() {
this.callApi()
// Need to wait for the call to resolve
// Do something else
}
}
As you can see, firstMethod
and secondMethod
both rely on callApi
but are supposed to do a different thing once the request is done. I prefer to split this logic in different functions instead of using conditions in the callApi
method.
Is there a way to do this without having to add this logic inside of the
callApi
?