Currently, I am utilizing Vue and executing multiple calls using axios
. However, I find the structure of my code to be messy and am seeking alternative approaches. While my current implementation functions as intended, I believe there might be a more efficient way to organize it. Therefore, I would greatly appreciate any insights or suggestions on how to improve it.
I feel like my existing setup is convoluted, and I am inclined to explore better alternatives:
login: function() {
this.toggleLoading();
this.findUser().then(success => {
if(success) {
this.validateSomething().then(success => {
if(success) {
this.auth();
}
});
}
});
}
The methods findUser
and validateSomething
are currently structured as follows:
findUser: function() {
return axios
.post('/find-user', {
id: this.id
})
.then(response => {
if(response.data.success) {
return true;
} else {
this.addErrors(response.data.message);
this.toggleLoading();
return false;
}
})
.catch(this.catchAll)
},
I prefer not to merge the findUser
and validateSomething
functions so that I can utilize them independently.
However, my goal is to achieve a structure similar to this instead:
login: function() {
this.toggleLoading();
if(!this.findUser()) {
return false;
}
if(this.validateSomething()) {
this.auth();
}
}
Can anyone provide recommendations regarding best practices for this specific scenario?