I would like to know how to handle promises properly when there is a condition involved too.
For example I have this code:
if(something == true) {
loginObj.$login("anonymous").then(function(){ //handling the promise
//do something with the promise
//do something line 1
//do something line 2
//do something line 3
//do something line 4
//do something line 5
});
}
else {
//do the same thing line 1
//do the same thing line 2
//do the same thing line 3
//do the same thing line 4
//do the same thing line 5
}
I hope you can see my problem. If something is true then I will have to wait for the promise to resolve before executing my lines of code. However, my else block contains nearly all the same lines of code but I'm having to repeat myself.
Why can I do to avoid this duplication?